public ComponentCompileResult Compile(Stream input, Stream output, IResourceProvider resourceProvider,
                                              CompileOptions options)
        {
            Log.LogDebug($"Compiling {input.StreamToString()}");

            var runner = new CompileStageRunner(new ICompileStage[]
            {
                new LoadFromXmlCompileStage(),
                new SetIconsCompileStage(),
                new OutputCdcomCompileStage()
            }, resourceProvider);

            ComponentCompileResult result;

            try
            {
                result = runner.Run(input, output, options);

                Log.LogDebug($"Compiled to {output.StreamToString()}");
            }
            catch (Exception ex)
            {
                result = new ComponentCompileResult
                {
                    Success = false
                };

                Log.LogError("An error occurred.", ex);
            }

            return(result);
        }
Exemple #2
0
        private static ExtendedCompileResult CompileComponent(string inputPath, CliCompileOptions cliOptions)
        {
            var compiler = new CompilerService();

            var resources = new MappedDirectoryResourceProvider(cliOptions.DefaultIconPath);

            // Icon paths are specified as:
            // --icon resourceName fileName
            // --icon wire_32 wire_32.png wire_64 wire_64.png
            for (int i = 0; i < cliOptions.IconPaths.Count; i += 2)
                resources.Mappings.Add(cliOptions.IconPaths[i], cliOptions.IconPaths[i + 1]);

            var options = new CompileOptions()
            {
                CertificateThumbprint = cliOptions.CertificateThumbprint
            };

            if (cliOptions.Sign && cliOptions.CertificateThumbprint == null)
                options.CertificateThumbprint = SelectCertificate();

            string outputPath = GetOutputPath(inputPath, cliOptions);

            var outputDirectory = Path.GetDirectoryName(outputPath);
            if (!Directory.Exists(outputDirectory))
                Directory.CreateDirectory(outputDirectory);

            ComponentCompileResult result;
            using (var input = File.OpenRead(inputPath))
            using (var output = File.OpenWrite(outputPath))
            {
                result = compiler.Compile(input, output, resources, options);
            }

            var extendedResult = new ExtendedCompileResult(result)
            {
                Input = inputPath
            };

            if (result.Success)
            {
                Console.WriteLine("{0} -> {1}", Path.GetFullPath(inputPath), Path.GetFullPath(outputPath));
                extendedResult.Output = outputPath;
            }

            // Generate preview
            if (cliOptions.Preview != null)
            {
                string previewPath = GetPreviewPath(inputPath, cliOptions);
                string previewDirectory = Path.GetDirectoryName(previewPath);
                if (!Directory.Exists(previewDirectory))
                    Directory.CreateDirectory(previewDirectory);

                var preview = PreviewRenderer.GetSvgPreview(result.Description, null, true);
                File.WriteAllBytes(previewPath, preview);
            }

            return extendedResult;
        }