public async Task <bool> ExecuteAsync(CancellationToken cancellationToken, IProgress <string> progress)
        {
            var viewModel = new ConfigurationViewModel(Configuration);

            if (!viewModel.CanBuild())
            {
                Log.Error($"cannot build the configuration [{Configuration.Name}] because of validation errors.");
                foreach (var error in viewModel.ValidationErrors)
                {
                    Log.Error(error);
                }

                return(false);
            }
            if (Configuration.OutputFolders.Count == 0)
            {
                Log.Error($"No output paths specified.");
                progress.Report($"No output paths specified.");
                await Task.Delay(3000, cancellationToken);

                return(false);
            }

            var tempFolder = CreateTempFolder();
            var context    = new SolutionRewriteContext(cancellationToken, progress, Configuration);

            if (!Directory.Exists(tempFolder))
            {
                progress.Report($"Failed to create temp folder");
                await Task.Delay(3000, cancellationToken);

                Log.Error($"Failed to create temp folder");
                return(false);
            }

            var explorer = await SolutionExplorer.CreateAsync(SolutionPath, progress, cancellationToken);

            await CopySolutionAsync(context, explorer, tempFolder);

            if (!await RewriteSolutionAsync(context, tempFolder))
            {
                progress.Report($"Solution rewrite of {SolutionPath} failed.{Environment.NewLine}See logs for details.");
                return(false);
            }

            await DistributeArtifacts(cancellationToken, context, tempFolder);

            Log.Info($"Deleting temporary working folder {tempFolder}.");
            Directory.Delete(tempFolder, true);

            Log.Info($"Execution finished succesfully.");

            return(true);
        }
Esempio n. 2
0
        private void CreateRootVsTemplate(SolutionRewriteContext context, ProjectRewriteCache cache, string templatePath)
        {
            var template = new VsTemplate();

            template.Type = Constants.VsTemplate.ProjectTypes.ProjectGroup;
            template.TemplateData.Hidden             = context.Configuration.HideSubProjects;
            template.TemplateData.CreateInPlace      = context.Configuration.CreateInPlace;
            template.TemplateData.CreateNewFolder    = context.Configuration.CreateNewFolder;
            template.TemplateData.DefaultName        = context.Configuration.DefaultName;
            template.TemplateData.Description        = context.Configuration.Description;
            template.TemplateData.Name               = context.Configuration.Name;
            template.TemplateData.ProvideDefaultName = context.Configuration.ProvideDefaultName;
            template.TemplateData.CodeLanguage       = context.Configuration.CodeLanguage;
            template.TemplateData.Icon               = PackageHelper.GetConfigurationIcon(context.Configuration);

            template.TemplateContent = BuildRootTemplateContent(cache);

            SaveTemplate(template, templatePath);
        }
        private async Task DistributeArtifacts(CancellationToken cancellationToken, SolutionRewriteContext context, string tempFolder)
        {
            Log.Info($"Deploying to the following destinations: {Environment.NewLine}{string.Join(Environment.NewLine, Configuration.OutputFolders)}");

            if (Configuration.ZipContents)
            {
                Log.Info("Zip distribution is enabled.");
                var zipName = Path.GetTempFileName();
                try
                {
                    Log.Debug($"Creating zip file with contents of {tempFolder} at {zipName}.");

                    ZipHelper.ZipFolderContents(tempFolder, zipName);

                    foreach (var destinationFolder in Configuration.OutputFolders)
                    {
                        var destinationName = Path.Combine(destinationFolder, $"{Configuration.ArtifactName}.zip");
                        Log.Info($"Copying file {zipName} to {destinationName}.");
                        File.Copy(zipName, destinationName, true);
                        await Task.Delay(20, cancellationToken);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    throw;
                }
                finally
                {
                    Log.Info($"Deleting tmp file {zipName}.");
                    File.Delete(zipName);
                }
            }
            else
            {
                foreach (var folder in Configuration.OutputFolders)
                {
                    var destination = Path.Combine(folder, Configuration.ArtifactName);
                    FileHelper.CopyFolderContents(context.CancellationToken, tempFolder, destination);
                    await Task.Delay(20, cancellationToken);
                }
            }
        }
        private Task CopySolutionAsync(SolutionRewriteContext context, SolutionExplorer sourceExplorer, string destinationPath)
        {
            Log.Debug($"Copying solution from  \"{sourceExplorer.SolutionPath}\" to \"{destinationPath}\".");
            context.CancellationToken.ThrowIfCancellationRequested();

            var endsWithComparer = new EndsWithComparer();
            var blackList        = Configuration.GetFileCopyBlackListElements().Select(s => s.TrimStart('*')).ToImmutableHashSet();
            var sourceFiles      = new HashSet <string>(
                sourceExplorer
                .GetAllReferencedDocuments()
                .Concat(sourceExplorer.GetAllAdditiontalDocuments())
                .Concat(sourceExplorer.GetAllProjectFiles())
                .Concat(new [] { sourceExplorer.SolutionPath })
                );

            foreach (var sourceFile in sourceFiles)
            {
                if (context.CancellationToken.IsCancellationRequested)
                {
                    return(Task.CompletedTask);
                }

                var relativePath = GetRelativePath(SolutionPath, sourceFile);
                var destFileName = Path.Combine(destinationPath, relativePath);
                var fileInfo     = new FileInfo(destFileName);
                if (blackList.Contains(fileInfo.Name, endsWithComparer))
                {
                    continue;
                }

                if (!fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }
                File.Copy(sourceFile, destFileName);
            }

            return(Task.CompletedTask);
        }
Esempio n. 5
0
 public SolutionRewriter(SolutionRewriteContext context, string folder)
 {
     Context = context;
     Folder  = folder;
 }
Esempio n. 6
0
 public SolutionTemplateContentBuilder(ProjectRewriteCache cache, SolutionRewriteContext context)
 {
     Cache   = cache;
     Context = context;
 }
        private async Task <bool> RewriteSolutionAsync(SolutionRewriteContext context, string destinationFolder)
        {
            var rewriter = new SolutionRewriter(context, destinationFolder);

            return(await rewriter.RewriteAsync());
        }