示例#1
0
        private List <FileFullPath> RemoveDuplicateFiles(List <FileFullPath> filesToZip, FullPath baseDir)
        {
            ZipMetadata         metadata = new ZipMetadata();
            List <FileFullPath> list     = new List <FileFullPath>();

            while (filesToZip.Count > 0)
            {
                FileFullPath current       = filesToZip[filesToZip.Count - 1];
                var          currentDebase = current.ToFullPath().DebasePath(baseDir);

                filesToZip.RemoveAt(filesToZip.Count - 1);
                ZipMetadataItem metaItem = new ZipMetadataItem {
                    FileName = currentDebase
                };
                metaItem.DestinationFiles.Add(currentDebase);
                metadata.Items.Add(metaItem);
                list.Add(current);

                byte[]   firstHash   = CalculateHash(current.ToString());
                FileInfo currentInfo = new FileInfo(current.ToString());

                for (int i = filesToZip.Count - 1; i >= 0; i--)
                {
                    FileFullPath tmp = filesToZip[i];

                    if (tmp.FileName != current.FileName)
                    {
                        continue;
                    }

                    FileInfo tmpInfo = new FileInfo(tmp.ToString());

                    if (tmpInfo.Length != currentInfo.Length)
                    {
                        continue;
                    }

                    byte[] secondHash = CalculateHash(tmp.ToString());

                    if (!HashEqual(firstHash, secondHash))
                    {
                        continue;
                    }

                    metaItem.DestinationFiles.Add(tmp.ToFullPath().DebasePath(baseDir));
                    filesToZip.RemoveAt(i);
                }
            }

            string metadataFile = Path.Combine(baseDir.ToString(), MetadataFileName);

            File.WriteAllText(metadataFile, JsonConvert.SerializeObject(metadata));
            list.Add(new FileFullPath(metadataFile));
            return(list);
        }
示例#2
0
文件: NUnitTask.cs 项目: breki/flubu
        private void SetAssemblyFileNameAndWorkingDirFromProjectName(ITaskContext context)
        {
            if (projectName != null)
            {
                VSSolution solution           = context.Properties.Get <VSSolution>(BuildProps.Solution);
                string     buildConfiguration = context.Properties.Get <string>(BuildProps.BuildConfiguration);

                VSProjectWithFileInfo project =
                    (VSProjectWithFileInfo)solution.FindProjectByName(projectName);
                FileFullPath projectTarget = project.ProjectDirectoryPath.CombineWith(project.GetProjectOutputPath(buildConfiguration))
                                             .AddFileName("{0}.dll", project.ProjectName);

                testAssemblyFileName = projectTarget.ToString();
                workingDirectory     = Path.GetDirectoryName(projectTarget.ToString());
            }
        }
示例#3
0
        private void AddFileToZip(FileFullPath fileName, string debasedFileName, ZipOutputStream zipStream)
        {
            using (FileStream fileStream = File.OpenRead(fileName.ToString()))
            {
                string fileHeader = String.Empty;
                string fileFooter = String.Empty;

                //if (zipFileHeaderCallback != null)
                //    fileHeader = zipFileHeaderCallback(fileName);

                //if (zipFileFooterCallback != null)
                //    fileFooter = zipFileFooterCallback(fileName);

                ZipEntry entry = new ZipEntry(debasedFileName);
                entry.DateTime = File.GetLastWriteTime(fileName.ToString());
                entry.Size     = fileStream.Length + fileHeader.Length + fileFooter.Length;
                zipStream.PutNextEntry(entry);

                int sourceBytes;

                WriteTextToZipStream(fileHeader, zipStream);

                while (true)
                {
                    sourceBytes = fileStream.Read(buffer, 0, buffer.Length);

                    if (sourceBytes == 0)
                    {
                        break;
                    }

                    zipStream.Write(buffer, 0, sourceBytes);
                }

                WriteTextToZipStream(fileFooter, zipStream);
            }
        }
示例#4
0
文件: Copier.cs 项目: breki/flubu
        public void Copy(FileFullPath sourceFileName, FileFullPath destinationFileName)
        {
            string directoryName = destinationFileName.Directory.ToString();

            if (false == String.IsNullOrEmpty(directoryName))
            {
                if (false == Directory.Exists(directoryName))
                {
                    taskContext.WriteDebug("Creating directory '{0}'", directoryName);
                    Directory.CreateDirectory(directoryName);
                }
            }

            taskContext.WriteDebug("Copying file '{0}' to '{1}'", sourceFileName, destinationFileName);
            File.Copy(sourceFileName.ToString(), destinationFileName.ToString(), true);
        }
示例#5
0
        public void ZipFiles(
            FileFullPath zipFileName,
            FullPath baseDir,
            int?compressionLevel,
            IEnumerable <FileFullPath> filesToZip)
        {
            taskContext.WriteInfo("Zipping {0}", zipFileName);

            CreateDirectoryTask createDirectoryTask = new CreateDirectoryTask(
                zipFileName.Directory.ToString(),
                false);

            createDirectoryTask.Execute(taskContext);

            using (FileStream zipFileStream = new FileStream(
                       zipFileName.ToString(),
                       FileMode.Create,
                       FileAccess.ReadWrite,
                       FileShare.None))
            {
                using (ZipOutputStream zipStream = new ZipOutputStream(zipFileStream))
                {
                    if (compressionLevel.HasValue)
                    {
                        zipStream.SetLevel(compressionLevel.Value);
                    }

                    buffer = new byte[1024 * 1024];

                    foreach (FileFullPath fileName in filesToZip)
                    {
                        LocalPath debasedFileName = fileName.ToFullPath().DebasePath(baseDir);
                        string    cleanName       = ZipEntry.CleanName(debasedFileName.ToString());

                        //environment.LogMessage("Zipping file '{0}'", basedFileName);
                        AddFileToZip(fileName, cleanName, zipStream);
                    }
                }
            }
        }
示例#6
0
        public static void TargetRunTestsNUnit(
            ITaskContext context,
            string projectName)
        {
            VSSolution solution           = context.Properties.Get <VSSolution>(BuildProps.Solution);
            string     buildConfiguration = context.Properties.Get <string>(BuildProps.BuildConfiguration);

            VSProjectWithFileInfo project =
                (VSProjectWithFileInfo)solution.FindProjectByName(projectName);
            FileFullPath projectTarget = project.ProjectDirectoryPath.CombineWith(project.GetProjectOutputPath(buildConfiguration))
                                         .AddFileName("{0}.dll", project.ProjectName);

            IRunProgramTask task = new RunProgramTask(
                context.Properties[BuildProps.NUnitConsolePath])
                                   .AddArgument(projectTarget.ToString())
                                   .AddArgument("/labels")
                                   .AddArgument("/trace=Verbose")
                                   .AddArgument("/nodots")
                                   .AddArgument("/noshadow");

            task.Execute(context);
        }
示例#7
0
        public void Copy(FileFullPath sourceFileName, FileFullPath destinationFileName)
        {
            var directoryName = destinationFileName.Directory.ToString();

            if (!string.IsNullOrEmpty(directoryName))
            {
                if (!Directory.Exists(directoryName))
                {
                    if (_logCopiedFiles)
                    {
                        _taskContext.LogInfo($"Creating directory '{directoryName}'");
                    }

                    Directory.CreateDirectory(directoryName);
                }
            }

            if (_logCopiedFiles)
            {
                _taskContext.LogInfo($"Copying file '{sourceFileName}' to '{destinationFileName}'");
            }

            File.Copy(sourceFileName.ToString(), destinationFileName.ToString(), true);
        }
示例#8
0
 public void Copy(FileFullPath sourceFileName, FileFullPath destinationFileName)
 {
     copiedFiles.Add(new KeyValuePair <string, string>(sourceFileName.ToString(), destinationFileName.ToString()));
 }
示例#9
0
        protected override void DoExecute(ITaskContext context)
        {
            FullPath packagesDir = new FullPath(context.Properties.Get(BuildProps.ProductRootDir, "."));

            packagesDir = packagesDir.CombineWith(context.Properties.Get <string> (BuildProps.BuildDir));

            FileFullPath destNuspecFile = packagesDir.AddFileName("{0}.nuspec", packageId);

            context.WriteInfo("Preparing the {0} file", destNuspecFile);
            ReplaceTokensTask task = new ReplaceTokensTask(
                nuspecFileName,
                destNuspecFile.ToString());

            task.AddTokenValue("version", context.Properties.Get <Version> (BuildProps.BuildVersion).ToString());
            task.Execute(context);

            // package it
            context.WriteInfo("Creating a NuGet package file");
            string           nugetWorkingDir = destNuspecFile.Directory.ToString();
            NuGetCmdLineTask nugetTask       = new NuGetCmdLineTask("pack", nugetWorkingDir);

            nugetTask
            .AddArgument(destNuspecFile.FileName);

            nugetTask.AddVerbosityArgument(NuGetCmdLineTask.NuGetVerbosity.Detailed);

            if (BasePath != null)
            {
                nugetTask.AddArgument("-BasePath").AddArgument(BasePath);
            }

            nugetTask.Execute(context);

            string nupkgFileName = ConstructNupkgFileName(context);

            context.WriteInfo("NuGet package file {0} created", nupkgFileName);

            // do not push new packages from a local build
            if (context.IsInteractive && !AllowPushOnInteractiveBuild)
            {
                return;
            }

            if (apiKeyFunc == null)
            {
                throw new InvalidOperationException("NuGet API key was not provided");
            }

            string apiKey = apiKeyFunc(context);

            if (apiKey == null)
            {
                context.WriteInfo("API key function returned null, skipping pushing of the NuGet package.");
                return;
            }

            // publish the package file
            context.WriteInfo("Pushing the NuGet package to the repository");

            nugetTask = new NuGetCmdLineTask("push", nugetWorkingDir);
            nugetTask
            .AddArgument(nupkgFileName)
            .AddArgument(apiKey)
            .AddArgument("-Source").AddArgument(nuGetServerUrl)
            .AddVerbosityArgument(NuGetCmdLineTask.NuGetVerbosity.Detailed);

            nugetTask
            .Execute(context);
        }
        protected override int DoExecute(ITaskContextInternal context)
        {
            FullPath packagesDir = new FullPath(context.Properties.Get(BuildProps.ProductRootDir, "."));

            packagesDir = packagesDir.CombineWith(context.Properties.Get <string>(DotNetBuildProps.BuildDir));

            FileFullPath destNuspecFile = packagesDir.AddFileName("{0}.nuspec", _packageId);

            DoLogInfo($"Preparing the {destNuspecFile} file");

            new ReplaceTokensTask(_nuspecFileName)
            .Replace("version", context.Properties.GetBuildVersion().BuildVersionWithQuality(3))
            .UseToken("$")
            .ToDestination(destNuspecFile.ToString())
            .ExecuteVoid(context);

            // package it
            DoLogInfo("Creating a NuGet package file");
            string           nugetWorkingDir = destNuspecFile.Directory.ToString();
            NuGetCmdLineTask nugetTask       = new NuGetCmdLineTask("pack", nugetWorkingDir)
            {
                Verbosity = NuGetCmdLineTask.NuGetVerbosity.Detailed
            };

            nugetTask.WithArguments(destNuspecFile.FileName);

            if (BasePath != null)
            {
                nugetTask.WithArguments("-BasePath", BasePath);
            }

            nugetTask.ExecuteVoid(context);

            string nupkgFileName = string.Format(
                CultureInfo.InvariantCulture,
                "{0}.{1}.nupkg",
                _packageId,
                context.Properties.GetBuildVersion().BuildVersionWithQuality(3));

            DoLogInfo($"NuGet package file {nupkgFileName} created");

            // do not push new packages from a local build
            if (context.BuildServers().IsLocalBuild&& _skipPushOnLocalBuild)
            {
                context.LogInfo("pushing package on local build is disabled in build script...Skiping.");
                return(1);
            }

            if (_apiKeyFunc == null)
            {
                throw new InvalidOperationException("NuGet API key was not provided");
            }

            string apiKey = _apiKeyFunc(context);

            if (apiKey == null)
            {
                return(1);
            }

            // publish the package file
            DoLogInfo("Pushing the NuGet package to the repository");

            nugetTask = new NuGetCmdLineTask("push", nugetWorkingDir)
            {
                Verbosity = NuGetCmdLineTask.NuGetVerbosity.Detailed,
                ApiKey    = apiKey
            };

            if (_nuGetServerUrl != null)
            {
                nugetTask.WithArguments("-Source", _nuGetServerUrl);
            }

            nugetTask
            .WithArguments(nupkgFileName)
            .ExecuteVoid(context);

            return(0);
        }
示例#11
0
        private static void TargetNuGet(ITaskContext context, string nugetId)
        {
            FullPath packagesDir = new FullPath(context.Properties.Get(BuildProps.ProductRootDir, "."));

            packagesDir = packagesDir.CombineWith(context.Properties.Get <string>(BuildProps.BuildDir));

            string sourceNuspecFile = string.Format(
                CultureInfo.InvariantCulture,
                @"{0}\{0}.nuspec",
                nugetId);
            FileFullPath destNuspecFile = packagesDir.AddFileName("{0}.nuspec", nugetId);

            context.WriteInfo("Preparing the {0} file", destNuspecFile);
            ExpandPropertiesTask task = new ExpandPropertiesTask(
                sourceNuspecFile,
                destNuspecFile.ToString(),
                Encoding.UTF8,
                Encoding.UTF8);

            task.AddPropertyToExpand("version", context.Properties.Get <Version>(BuildProps.BuildVersion).ToString());
            task.Execute(context);

            // package it
            context.WriteInfo("Creating a NuGet package file");
            RunProgramTask progTask = new RunProgramTask(@"lib\NuGet\NuGet.exe");

            progTask.SetWorkingDir(destNuspecFile.Directory.ToString());
            progTask
            .AddArgument("pack")
            .AddArgument(destNuspecFile.FileName)
            .AddArgument("-Verbose")
            .Execute(context);

            string nupkgFileName = string.Format(
                CultureInfo.InvariantCulture,
                "{0}.{1}.nupkg",
                nugetId,
                context.Properties.Get <Version>(BuildProps.BuildVersion));

            context.WriteInfo("NuGet package file {0} created", nupkgFileName);

            string apiKeyFileName = "NuGet API key.txt";

            if (!File.Exists(apiKeyFileName))
            {
                context.WriteInfo("'NuGet API key.txt' does not exist, cannot publish the package.");
                return;
            }

            string apiKey = File.ReadAllText(apiKeyFileName);

            // publish the package file
            context.WriteInfo("Pushing the NuGet package to the repository");

            progTask = new RunProgramTask(@"lib\NuGet\NuGet.exe");
            progTask.SetWorkingDir(destNuspecFile.Directory.ToString());
            progTask
            .AddArgument("push")
            .AddArgument(nupkgFileName)
            .AddArgument(apiKey)
            .AddArgument("-Source")
            .AddArgument("http://packages.nuget.org/v1/")
            .Execute(context);
        }