<?xml version="1.0"?>
<!DOCTYPE project>

<project name="build-common-plugins-inspector.xml">
	<macrodef name="record-fail">
		<attribute name="message" />

		<sequential>
			<echo append="true" file="../plugins-inspector.log">@{message}${line.separator}</echo>
		</sequential>
	</macrodef>

	<target name="inspect-plugins">
		<delete file="../plugins-inspector.log" />

		<checksum file="build.xml" property="build.xml.checksum" />

		<if>
			<not>
				<equals arg1="${build.xml.checksum}" arg2="aa45183ecb2cda43d255c7070a6225de" />
			</not>
			<then>
				<record-fail message="build.xml is nonstandard." />
			</then>
		</if>

		<exec dir="." executable="git">
			<arg line="clean -d -f -x ." />
		</exec>

		<trycatch>
			<try>
				<antcall target="clean" />
			</try>
			<catch>
				<record-fail message="Unable to clean all plugins." />
			</catch>
		</trycatch>

		<trycatch>
			<try>
				<antcall target="compile" />
			</try>
			<catch>
				<record-fail message="Unable to compile all plugins." />
			</catch>
		</trycatch>

		<for param="module.name">
			<path>
				<dirset dir="." includes="*" />
			</path>
			<sequential>
				<if>
					<and>
						<available file="@{module.name}/docroot" />
						<available file="@{module.name}/src" />
					</and>
					<then>
						<record-fail message="Module @{module.name} must not have both a &quot;docroot&quot; and a &quot;src&quot; directory." />
					</then>
				</if>

				<if>
					<and>
						<not>
							<available file="@{module.name}/docroot" />
						</not>
						<not>
							<available file="@{module.name}/src" />
						</not>
					</and>
					<then>
						<record-fail message="Module @{module.name} must have a &quot;docroot&quot; or a &quot;src&quot; directory." />
					</then>
				</if>

				<if>
					<available file="@{module.name}/docroot/WEB-INF/liferay-hook.xml" />
					<then>
						<if>
							<and>
								<resourcecontains resource="@{module.name}/docroot/WEB-INF/liferay-hook.xml" substring="&lt;language-properties&gt;" />
								<not>
									<resourcecontains resource="@{module.name}/docroot/WEB-INF/liferay-hook.xml" substring="&lt;language-properties&gt;content/Language*.properties&lt;/language-properties&gt;" />
								</not>
							</and>
							<then>
								<record-fail message="@{module.name}/docroot/WEB-INF/liferay-hook.xml has an nonstandard language-properties element." />
							</then>
						</if>

						<if>
							<and>
								<resourcecontains resource="@{module.name}/docroot/WEB-INF/liferay-hook.xml" substring="&lt;custom-jsp-dir&gt;" />
								<not>
									<resourcecontains resource="@{module.name}/docroot/WEB-INF/liferay-hook.xml" substring="&lt;custom-jsp-dir&gt;/META-INF/custom_jsps&lt;/custom-jsp-dir&gt;" />
								</not>
							</and>
							<then>
								<record-fail message="@{module.name}/docroot/WEB-INF/liferay-hook.xml has a nonstandard custom-jsp-dir element." />
							</then>
						</if>
					</then>
				</if>

				<if>
					<resourcecontains resource="@{module.name}/docroot/WEB-INF/web.xml" substring="Invoker Filter" />
					<then>
						<record-fail message="@{module.name}/docroot/WEB-INF/web.xml contains generated code." />
					</then>
				</if>

				<if>
					<available file="@{module.name}/ivy.xml.MD5" />
					<then>
						<record-fail message="@{module.name}/ivy.xml.MD5 must not be checked into source control." />
					</then>
				</if>

				<if>
					<available file="@{module.name}/docroot/WEB-INF/lib" />
					<then>
						<record-fail message="@{module.name}/docroot/WEB-INF/lib must not be checked into source control." />
					</then>
				</if>

				<if>
					<available file="@{module.name}/docroot/WEB-INF/tld" />
					<then>
						<record-fail message="@{module.name}/docroot/WEB-INF/tld must not be checked into source control." />
					</then>
				</if>

				<if>
					<available file="@{module.name}/lib" />
					<then>
						<record-fail message="@{module.name}/lib must not be checked into source control." />
					</then>
				</if>
			</sequential>
		</for>

		<exec dir="." executable="git" outputproperty="git.command.output">
			<arg line="status ." />
		</exec>

		<if>
			<available file=".gitignore" />
			<then>
				<echo file=".gitignore">/*/bin
/*/classes
/*/ivy.xml.MD5
/*/javadoc
/*/lib
/*/test-classes
/*/test-results</echo>
			</then>
		</if>

		<if>
			<resourcecount count="0" when="gt">
				<fileset defaultexcludes="false" dir="." includes="**/.gitignore" />
			</resourcecount>
			<then>
				<record-fail message="Plugins must not have custom .gitignore files." />
			</then>
		</if>

		<if>
			<or>
				<contains string="${git.command.output}" substring="Changes not staged for commit" />
				<contains string="${git.command.output}" substring="Untracked files" />
			</or>
			<then>
				<record-fail message="Plugins must not generate modified files." />
			</then>
		</if>

		<trycatch>
			<try>
				<loadfile property="plugins-inspector.log.content" srcFile="../plugins-inspector.log" />

				<if>
					<equals arg1="${basedir.name}" arg2="test-broken" />
					<then>
						<checksum file="../plugins-inspector.log" property="plugins-inspector.log.actual.checksum" />
						<property name="plugins-inspector.log.expected.checksum" value="20988e2ed40f504cbc58c773e24b7fe5" />

						<if>
							<not>
								<equals arg1="${plugins-inspector.log.actual.checksum}" arg2="${plugins-inspector.log.expected.checksum}" />
							</not>
							<then>
								<copy file="../plugins-inspector.log" tofile="../plugins-inspector.log.test-broken" />

								<echo>${plugins-inspector.log.content}</echo>

								<fail>Plugins inspector is not detecting all errors. Expected &quot;${plugins-inspector.log.expected.checksum}&quot; but found &quot;${plugins-inspector.log.actual.checksum}&quot;.</fail>
							</then>
						</if>
					</then>
					<elseif>
						<equals arg1="${basedir.name}" arg2="test-working" />
						<then>
							<if>
								<available file="../plugins-inspector.log" />
								<then>
									<echo>${plugins-inspector.log.content}</echo>

									<fail>Plugins inspector is not detecting false errors.</fail>
								</then>
							</if>
						</then>
					</elseif>
					<else>
						<if>
							<available file="../plugins-inspector.log" />
							<then>
								<echo>${plugins-inspector.log.content}</echo>

								<fail>Plugins inspector detected errors.</fail>
							</then>
						</if>
					</else>
				</if>
			</try>
			<finally>
				<delete file="../plugins-inspector.log" />
			</finally>
		</trycatch>
	</target>
</project>