Ejemplo n.º 1
0
    public override bool Execute()
    {
        TaskLogger logger = new TaskLogger(this);

        List <string> compileGeneratedFiles;
        List <string> embeddedResourceGeneratedFiles;
        List <string> filesToDelete;

        bool debugMode = (ConditionalCompilationSymbols.Contains("PRESHARP_DEBUG") || Environment.GetEnvironmentVariable("PRESHARP_DEBUG") != null) &&
                         !(ConditionalCompilationSymbols.Contains("DISABLE_PRESHARP_DEBUG") || Environment.GetEnvironmentVariable("DISABLE_PRESHARP_DEBUG") != null);

        PreSharpEntryPoint.Process(
            logger,
            InPlaceFiles.Select(taskItem => taskItem.ItemSpec),
            TemplateFiles.Select(taskItem => taskItem.ItemSpec),
            TemplateIncludeFiles.Select(taskItem => taskItem.ItemSpec),
            TemplateLibraryFiles.Select(taskItem => taskItem.ItemSpec),
            DependencyPaths.Select(taskItem => taskItem.ItemSpec),
            Path.GetFullPath(Path.Combine(Path.GetDirectoryName(ProjectPath), OutputDirectory)),
            out compileGeneratedFiles,
            out embeddedResourceGeneratedFiles,
            out filesToDelete,
            /*createNewAppDomain*/ false,
            /*debugMode*/ debugMode,
            ConditionalCompilationSymbols);

        CompileGeneratedFiles          = compileGeneratedFiles.Select(file => new TaskItem(file)).ToArray();
        EmbeddedResourceGeneratedFiles = embeddedResourceGeneratedFiles.Select(file => new TaskItem(file)).ToArray();
        FilesToDelete = filesToDelete.Select(file => new TaskItem(file)).ToArray();

        return(logger.Success);
    }
Ejemplo n.º 2
0
        private bool BuildPackage(ITaskItem item)
        {
            var res = true;

            try
            {
                var idenity            = item.GetMetadata("Identity");
                var filename           = Path.GetFileName(idenity);
                var packageName        = ProjectName + ".pnp";
                var packedTemplateName = Path.GetFileNameWithoutExtension(filename) + ".xml";

                LogMessage($"Packing template={filename}, package={packageName}");

                XMLFileSystemTemplateProvider provider = new XMLFileSystemTemplateProvider(ProjectDir, "");
                var fsConnector = provider.Connector;
                var template    = provider.GetTemplate(idenity);

                var configManager = new ConfigurationManager();
                var config        = configManager.GetProjectConfiguration(ProjectDir);

                var outFile = Path.Combine(ProjectDir, OutDir, packageName);
                OpenXMLConnector openXml = new OpenXMLConnector(outFile, fsConnector, config.Author);

                //write files
                var files = template.Files != null?template.Files.Select(f => f.Src.ToLower()).ToList() : new List <string>();

                if (TemplateFiles?.Length > 0)
                {
                    files = files.Union(TemplateFiles.Select(t => t.GetMetadata("Identity")?.ToLower())).ToList();
                }

                foreach (var file in files)
                {
                    LogMessage($"Packing file={file}, package={packageName}");
                    var fileName  = Path.GetFileName(file);
                    var container = Path.GetDirectoryName(file);
                    using (var stream = fsConnector.GetFileStream(fileName, container))
                    {
                        if (stream != null)
                        {
                            openXml.SaveFileStream(fileName, container, stream);
                        }
                        else
                        {
                            throw new FileNotFoundException($"Not found: {Path.Combine(ProjectDir, file)}");
                        }
                    }
                }

                var xml = template.ToXML();
                using (var stream = GetStream(xml))
                {
                    openXml.SaveFileStream(packedTemplateName, stream);
                }

                openXml.Commit();
                LogMessage($"Packed successfully.");
            }
            catch (Exception e)
            {
                LogError(e);
                res = false;
            }
            return(res);
        }