Commits

Mauricio Scheffer committed 68d270d

Lockfile to prevent concurrent access to NuGet.zip

Comments (0)

Files changed (1)

BuildMagic.targets

     <BuildMagicNuGetUrl Condition="'$(BuildMagicNuGetUrl)' == ''">http://nuget.org/api/v1/package/NuGet.CommandLine/$(BuildMagicNuGetVersion)</BuildMagicNuGetUrl>
     <BuildMagicNuGetFolder Condition="'$(BuildMagicNuGetFolder)' == ''">$(MSBuildThisFileDirectory)/bin</BuildMagicNuGetFolder>
     <BuildMagicNuGetLocation Condition="'$(BuildMagicNuGetLocation)' == ''">$(BuildMagicNuGetFolder)/NuGet.exe</BuildMagicNuGetLocation>
+    <BuildMagicLock Condition="'$(BuildMagicLock)' == ''">$(MSBuildThisFileDirectory)/BuildMagic.lock</BuildMagicLock>
     <BuildMagicPackagesConfig Condition="'$(BuildMagicPackagesConfig)' == ''">$(MSBuildProjectDirectory)/packages.config</BuildMagicPackagesConfig>
     <BuildMagicSources Condition="'$(BuildMagicSources)' == ''"></BuildMagicSources>
     <BuildMagicPackages Condition="'$(BuildMagicPackages)' == ''">$(MSBuildThisFileDirectory)/../packages</BuildMagicPackages>
       <ZipFile ParameterType="System.String" Required="true" />
       <PathInZipFile ParameterType="System.String" Required="true" />
       <DestinationFile ParameterType="System.String" Required="true" />
+      <LockFile ParameterType="System.String" Required="true" />
     </ParameterGroup>
     <Task>
       <Reference Include="WindowsBase" />
+      <Using Namespace="System.Threading"/>
       <Code Type="Fragment" Language="cs">
         <![CDATA[
-          using (var client = new System.Net.WebClient())
-          {
-            Directory.CreateDirectory(Path.GetDirectoryName(ZipFile));
-            client.DownloadFile(Address, ZipFile);
+          var locked = false;
+          while (File.Exists(LockFile)) {
+            locked = true;
+            Thread.Sleep(500);
           }
-          using (var pkg = System.IO.Packaging.ZipPackage.Open(ZipFile, FileMode.Open))
-          {
-            var part = pkg.GetParts().First(x => x.Uri.ToString() == PathInZipFile);
-            using (var stream = part.GetStream(FileMode.Open))
+          if (locked) return true;
+
+          try {
+            File.WriteAllText(LockFile, "");
+            using (var client = new System.Net.WebClient())
             {
-              Directory.CreateDirectory(Path.GetDirectoryName(DestinationFile));
-              using (var output = File.Open(DestinationFile, FileMode.Create))
+              Directory.CreateDirectory(Path.GetDirectoryName(ZipFile));
+              client.DownloadFile(Address, ZipFile);
+            }
+            using (var pkg = System.IO.Packaging.ZipPackage.Open(ZipFile, FileMode.Open))
+            {
+              var part = pkg.GetParts().First(x => x.Uri.ToString() == PathInZipFile);
+              using (var stream = part.GetStream(FileMode.Open))
               {
-                stream.CopyTo(output);
+                Directory.CreateDirectory(Path.GetDirectoryName(DestinationFile));
+                using (var output = File.Open(DestinationFile, FileMode.Create))
+                {
+                  stream.CopyTo(output);
+                }
               }
+              return true;
             }
-            return true;
+          } finally {
+            File.Delete(LockFile);
           }
         ]]>
       </Code>
     <PropertyGroup>
       <BuildMagicNuGetZip>$(BuildMagicNuGetLocation).zip</BuildMagicNuGetZip>
     </PropertyGroup>
-    <BuildMagicDownloadAndExtract Address="$(BuildMagicNuGetUrl)" ZipFile="$(BuildMagicNuGetZip)" PathInZipFile="/tools/NuGet.exe" DestinationFile="$(BuildMagicNuGetLocation)"/>
+    <BuildMagicDownloadAndExtract Address="$(BuildMagicNuGetUrl)" ZipFile="$(BuildMagicNuGetZip)" PathInZipFile="/tools/NuGet.exe" DestinationFile="$(BuildMagicNuGetLocation)" LockFile="$(BuildMagicLock)"/>
   </Target>
 
   <Target Name="BuildMagicRestore" Condition="Exists('$(BuildMagicPackagesConfig)')" DependsOnTargets="BuildMagicInstallNuget">