Exemplo n.º 1
0
        /// <summary>
        /// Packs the specified template and its referenced artifacts for use
        /// in a Template Spec.
        /// </summary>
        /// <param name="rootTemplateFilePath">
        /// The path to the ARM Template .json file to pack
        /// </param>
        /// <param name="uiFormDefinitionFilePath">
        /// The path to the UI Form Definition .json to pack (if any)
        /// </param>
        public static PackagedTemplate Pack(string rootTemplateFilePath,
                                            string uiFormDefinitionFilePath)
        {
            rootTemplateFilePath = Path.GetFullPath(rootTemplateFilePath);
            PackingContext context = new PackingContext(
                Path.GetDirectoryName(rootTemplateFilePath)
                );

            PackArtifacts(rootTemplateFilePath, context, out JObject templateObj);
            var packagedTemplate = new PackagedTemplate
            {
                RootTemplate = templateObj,
                Artifacts    = context.Artifacts.ToArray()
            };

            // If a UI Form Definition file path was provided to us, make sure we package the
            // UI Form Definition as well:

            if (!string.IsNullOrWhiteSpace(uiFormDefinitionFilePath))
            {
                string uiFormDefinitionJson = FileUtilities.DataStore.ReadFileAsText(uiFormDefinitionFilePath);
                packagedTemplate.UIFormDefinition = JObject.Parse(uiFormDefinitionJson);
            }

            return(packagedTemplate);
        }
        /// <summary>
        /// Packs the specified template and its referenced artifacts for use
        /// in a Template Spec.
        /// </summary>
        /// <param name="rootTemplateFilePath">
        /// The path to the ARM Template .json file to pack
        /// </param>
        public static PackagedTemplate Pack(string rootTemplateFilePath)
        {
            rootTemplateFilePath = Path.GetFullPath(rootTemplateFilePath);
            PackingContext context = new PackingContext(
                Path.GetDirectoryName(rootTemplateFilePath)
                );

            PackArtifacts(rootTemplateFilePath, context, out JObject templateObj);
            return(new PackagedTemplate
            {
                RootTemplate = templateObj,
                Artifacts = context.Artifacts.ToArray()
            });
        }
Exemplo n.º 3
0
        internal void Pack()
        {
            Logger.Message("开始打包");

            var packingFolder = _intermediateDirectory;

            PrepareEmptyDirectory(packingFolder);

            var projectFile = _projectFile;

            var projectName = Path.GetFileNameWithoutExtension(projectFile);

            if (string.IsNullOrEmpty(projectName))
            {
                Logger.Error("从项目文件无法拿到项目文件名 " + projectFile);
                return;
            }

            var projectFolder = Path.GetDirectoryName(projectFile);

            if (string.IsNullOrEmpty(projectFolder))
            {
                Logger.Error("无法拿到项目文件所在文件夹");
                return;
            }

            var buildProps = BuildProps;

            IPackFlow?current = null;

            try
            {
                foreach (var packer in _packers)
                {
                    current = packer;
                    var context = new PackingContext
                                  (
                        Logger,
                        projectFile,
                        projectName,
                        PackageId,
                        _packageVersion,
                        _packageOutputPath,
                        packingFolder,
                        PackagedProjectFile,
                        _multiTargetingPackageInfo
                                  )
                    {
                        BuildProps = buildProps,
                    };
                    packer.Pack(context);
                }

                Logger.Message("打包完成");
            }
            catch (PackingException ex)
            {
                Logger.Error(ex);
            }
            catch (Exception ex)
            {
                Logger.Error($"生成源码包: {current?.GetType().Name}: {ex}");
            }
        }
        /// <summary>
        /// Recursively packs the specified template and its referenced artifacts and
        /// adds the artifacts to the current packing context.
        /// </summary>
        /// <param name="templateAbsoluteFilePath">
        /// The path to the ARM Template .json file to pack
        /// </param>
        /// <param name="context">
        /// The packing context of the current packing operation
        /// </param>
        /// <param name="artifactableTemplateObj">
        /// The packagable template object
        /// </param>
        private static void PackArtifacts(
            string templateAbsoluteFilePath,
            PackingContext context,
            out JObject artifactableTemplateObj)
        {
            string originalDirectory = context.CurrentDirectory;

            try
            {
                context.CurrentDirectory = Path.GetDirectoryName(
                    templateAbsoluteFilePath
                    );

                string  templateJson = FileUtilities.DataStore.ReadFileAsText(templateAbsoluteFilePath);
                JObject templateObj  = artifactableTemplateObj = JObject.Parse(templateJson);

                JObject[] templateLinkToArtifactObjs = GetTemplateLinksToArtifacts(
                    templateObj, includeNested: true);

                foreach (JObject templateLinkObj in templateLinkToArtifactObjs)
                {
                    string relativePath = ((string)templateLinkObj["relativePath"])?
                                          .TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

                    if (string.IsNullOrWhiteSpace(relativePath))
                    {
                        continue; // Throw here?
                    }

                    // This is a templateLink to a local template... Get the absolute path of the
                    // template based on its relative path from the current template directory and
                    // make sure it exists:

                    string absoluteLocalPath = Path.Combine(context.CurrentDirectory, relativePath);
                    if (!File.Exists(absoluteLocalPath))
                    {
                        throw new FileNotFoundException(absoluteLocalPath);
                    }

                    // Let's make sure we're not referencing a file outside of our root directory
                    // heirarchy. We won't allow such references for security purposes:

                    if (!absoluteLocalPath.StartsWith(
                            context.RootTemplateDirectory + Path.DirectorySeparatorChar,
                            StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidOperationException(
                                  $"Unable to handle the reference to file '{absoluteLocalPath}' from " +
                                  $"'{templateAbsoluteFilePath}' because it exists outside of the root template " +
                                  $"directory of '{context.RootTemplateDirectory}'");
                    }

                    // Convert the template relative path to one that is relative to our root
                    // directory path, and then if we haven't already processed that template into
                    // an artifact elsewhere, we'll do so here...

                    string asRelativePath = AbsoluteToRelativePath(context.RootTemplateDirectory, absoluteLocalPath);
                    if (context.Artifacts.Any(prevAddedArtifact => prevAddedArtifact.Path.Equals(
                                                  asRelativePath, StringComparison.OrdinalIgnoreCase)))
                    {
                        continue; // We've already packed this artifact from elsewhere
                    }

                    PackArtifacts(absoluteLocalPath, context, out JObject templateObjForArtifact);

                    TemplateSpecArtifact artifact = new TemplateSpecTemplateArtifact
                    {
                        Path     = asRelativePath,
                        Template = templateObjForArtifact
                    };

                    context.Artifacts.Add(artifact);
                }
            }
            finally
            {
                context.CurrentDirectory = originalDirectory;
            }
        }
Exemplo n.º 5
0
        internal void Pack()
        {
            var packingFolder = _intermediateDirectory;

            PrepareEmptyDirectory(packingFolder);

            var projectFile = _projectFile;

            var projectName = Path.GetFileNameWithoutExtension(projectFile);

            if (string.IsNullOrEmpty(projectName))
            {
                Logger.Error("从项目文件无法拿到项目文件名 " + projectFile);
                return;
            }

            var projectFolder = Path.GetDirectoryName(projectFile);

            if (string.IsNullOrEmpty(projectFolder))
            {
                Logger.Error("无法拿到项目文件所在文件夹");
                return;
            }

            //Directory.Build.props
            var buildProps = BuildProps;

            if (string.IsNullOrWhiteSpace(projectName))
            {
                Logger.Error($"无法从 {projectFile} 解析出正确的项目名称。");
                return;
            }

            IPackFlow current = null;

            try
            {
                foreach (var packer in _packers)
                {
                    current = packer;
                    var context = new PackingContext
                                  (
                        Logger,
                        projectFile,
                        projectName,
                        _packageVersion,
                        _packageOutputPath,
                        packingFolder,
                        PackagedProjectFile
                                  )
                    {
                        BuildProps = buildProps,
                    };
                    packer.Pack(context);
                }
            }
            catch (PackingException ex)
            {
                Logger.Error(ex);
            }
            catch (Exception ex)
            {
                Logger.Error($"生成源码包: {current?.GetType().Name}: {ex}");
            }
        }