Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] target to simplify @(FileWrites) (dotne…
Browse files Browse the repository at this point in the history
…t#2344)

In our "MSBuild Best Practices" guide, the convention for using a
"stamp" file has a bit of boilerplate: `<Touch/>` the file, then add
the file to the `@(FileWrites)` group:

	<Target Name="_MyTarget"
	    Inputs="..."
	    Outputs="$(_AndroidStampDirectory)_MyTarget.stamp">
	  <!--...-->
	  <Touch Files="$(_AndroidStampDirectory)_MyTarget.stamp" AlwaysCreate="True" />
	  <ItemGroup>
	    <FileWrites Include="$(_AndroidStampDirectory)_MyTarget.stamp" />
	  </ItemGroup>
	</Target>

It is reasonably easy to mess this up.

If we add a new `_AddFilesToFileWrites` target which runs before the
`IncrementalClean` target and automatically adds
`$(_AndroidStampDirectory)*.stamp` to `@(FileWrites)`, we can
simplify things by only requiring the `<Touch/>`:

	<Target Name="_MyTarget"
	    Inputs="..."
	    Outputs="$(_AndroidStampDirectory)_MyTarget.stamp">
	  <!--...-->
	  <Touch Files="$(_AndroidStampDirectory)_MyTarget.stamp" AlwaysCreate="True" />
	</Target>

This should be less prone to mistakes.
  • Loading branch information
jonathanpeppers authored and jonpryor committed Oct 29, 2018
1 parent dbc1ad3 commit 3dc914d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
10 changes: 9 additions & 1 deletion Documentation/guides/MSBuildBestPractices.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,16 @@ We should also name the stamp file the same as the target, such as:
Outputs="$(_AndroidStampDirectory)_ResolveLibraryProjectImports.stamp">
<!-- ... -->
<Touch Files="$(_AndroidStampDirectory)_ResolveLibraryProjectImports.stamp" AlwaysCreate="True" />
</Target>
```

Do we need `FileWrites` here? Nope. The `_AddFilesToFileWrites`
target takes care of it, so we can't as easily mess it up:

```xml
<Target Name="_AddFilesToFileWrites" BeforeTargets="IncrementalClean">
<ItemGroup>
<FileWrites Include="$(_AndroidStampDirectory)_ResolveLibraryProjectImports.stamp" />
<FileWrites Include="$(_AndroidStampDirectory)*.stamp" />
</ItemGroup>
</Target>
```
Expand Down
20 changes: 7 additions & 13 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<Touch Files="$(_AndroidStampDirectory)_BuildAdditionalResourcesCache.stamp" AlwaysCreate="True" />
<ItemGroup>
<FileWrites Include="$(_AndroidResourcePathsCache)" Condition=" Exists ('$(_AndroidResourcePathsCache)') " />
<FileWrites Include="$(_AndroidStampDirectory)_BuildAdditionalResourcesCache.stamp" />
</ItemGroup>
</Target>

Expand Down Expand Up @@ -662,9 +661,6 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<CallTarget Targets="_CleanMonoAndroidIntermediateDir" Condition=" Exists ('$(_AndroidStampDirectory)_CleanIntermediateIfNuGetsChange.stamp') " />
<MakeDir Directories="$(_AndroidStampDirectory)" Condition=" !Exists('$(_AndroidStampDirectory)') " />
<Touch Files="$(_AndroidStampDirectory)_CleanIntermediateIfNuGetsChange.stamp" AlwaysCreate="true" />
<ItemGroup>
<FileWrites Include="$(_AndroidStampDirectory)_CleanIntermediateIfNuGetsChange.stamp" />
</ItemGroup>
</Target>

<Target Name="_ResolveMonoAndroidFramework" DependsOnTargets="GetReferenceAssemblyPaths" >
Expand Down Expand Up @@ -1323,9 +1319,6 @@ because xbuild doesn't support framework reference assemblies.
OutputImportDirectory="$(_AndroidLibrayProjectIntermediatePath)">
</ResolveLibraryProjectImports>
<Touch Files="$(_AndroidStampDirectory)_ResolveLibraryProjectImports.stamp" AlwaysCreate="True" />
<ItemGroup>
<FileWrites Include="$(_AndroidStampDirectory)_ResolveLibraryProjectImports.stamp" />
</ItemGroup>
</Target>

<Target Name="_CollectLibraryResourceDirectories"
Expand Down Expand Up @@ -1990,9 +1983,6 @@ because xbuild doesn't support framework reference assemblies.
/>
<Delete Files="@(ResolvedAssemblies->'$(MonoAndroidLinkerInputDir)%(Filename)%(Extension).mdb')" />
<Touch Files="$(_AndroidStampDirectory)_CopyIntermediateAssemblies.stamp" AlwaysCreate="True" />
<ItemGroup>
<FileWrites Include="$(_AndroidStampDirectory)_CopyIntermediateAssemblies.stamp" />
</ItemGroup>
</Target>

<Target Name="_CollectConfigFiles">
Expand Down Expand Up @@ -2298,9 +2288,6 @@ because xbuild doesn't support framework reference assemblies.
TargetFrameworkVersion="$(TargetFrameworkVersion)"
Manifest="$(IntermediateOutputPath)android\AndroidManifest.xml" />
<Touch Files="$(_AndroidStampDirectory)_GeneratePackageManagerJava.stamp" AlwaysCreate="True" />
<ItemGroup>
<FileWrites Include="$(_AndroidStampDirectory)_GeneratePackageManagerJava.stamp" />
</ItemGroup>
</Target>

<PropertyGroup>
Expand Down Expand Up @@ -3058,6 +3045,13 @@ because xbuild doesn't support framework reference assemblies.

<!-- Cleaning -->

<Target Name="_AddFilesToFileWrites" BeforeTargets="IncrementalClean">
<ItemGroup>
<!-- When following the naming convention for stamp files, this target handles FileWrites -->
<FileWrites Include="$(_AndroidStampDirectory)*.stamp" />
</ItemGroup>
</Target>

<Target Name="_CleanMsymArchive">
<GetAndroidPackageName ManifestFile="$(ProjectDir)$(AndroidManifest)" AssemblyName="$(AssemblyName)">
<Output TaskParameter="PackageName" PropertyName="_AndroidPackage" />
Expand Down

0 comments on commit 3dc914d

Please sign in to comment.