Esempio n. 1
0
        public async Task <TemplateContext> LoadRenderedContextAsync(string localTemplateFolder, string userConfigFilePath, string contextPath, string outputFolderPath)
        {
            if (localTemplateFolder == null)
            {
                throw new ArgumentNullException(nameof(localTemplateFolder));
            }

            if (contextPath == null)
            {
                throw new ArgumentNullException(nameof(contextPath));
            }

            if (outputFolderPath == null)
            {
                throw new ArgumentNullException(nameof(outputFolderPath));
            }

            var renderedContext = new TemplateContext();

            var result = await RunRenderContextScript(_redirector, _envInterpreterPath, localTemplateFolder, userConfigFilePath, outputFolderPath, contextPath);

            var contextJson = string.Join(Environment.NewLine, result.StandardOutputLines);
            var context     = (JToken)JObject.Parse(contextJson).SelectToken("cookiecutter");

            if (context != null)
            {
                foreach (JProperty prop in context)
                {
                    // Properties that start with underscore are for internal use,
                    // and cookiecutter doesn't prompt for them.
                    if (!prop.Name.StartsWithOrdinal("_"))
                    {
                        if (prop.Value.Type == JTokenType.String ||
                            prop.Value.Type == JTokenType.Integer ||
                            prop.Value.Type == JTokenType.Float)
                        {
                            renderedContext.Items.Add(new ContextItem(prop.Name, Selectors.String, prop.Value.ToString()));
                        }
                        else if (prop.Value.Type == JTokenType.Array)
                        {
                            var    elements = new List <string>();
                            JArray ar       = prop.Value as JArray;
                            foreach (JToken element in ar)
                            {
                                elements.Add(element.ToString());
                            }
                            renderedContext.Items.Add(new ContextItem(prop.Name, Selectors.List, elements[0], elements.ToArray()));
                        }
                        else
                        {
                            throw new InvalidOperationException(Strings.CookiecutterClient_UnsupportedJsonElementTypeForPorperty.FormatUI(prop.Name));
                        }
                    }
                    else if (prop.Name == "_visual_studio_post_cmds")
                    {
                        // List of commands to run after the folder is opened,
                        // or the files are added to the project.
                        // name and args are the values passed to DTE.ExecuteCommand
                        // args type:
                        //   - it should be of type array if the command is passed multiple arguments
                        //   - it can be of type string for the common single argument scenario,
                        //     this is equivalent to an array of a single element
                        // args value passed to DTE.ExecuteCommand will
                        // be concatenation of all values in the array, each one quoted as necessary
                        // (for example when a value contains a space char)
                        //
                        // cookiecutter._output_folder_path can be used to files inside generated project
                        //
                        // Examples:
                        // "_visual_studio_post_cmds": [
                        // {
                        //   "name": "Cookiecutter.ExternalWebBrowser",
                        //   "args": "https://docs.microsoft.com"
                        // },
                        // {
                        //   "name": "View.WebBrowser",
                        //   "args": "https://docs.microsoft.com"
                        // },
                        // {
                        //   "name": "View.WebBrowser",
                        //   "args": "{{cookiecutter._output_folder_path}}\\readme.html"
                        // },
                        // {
                        //   "name": "File.OpenFile",
                        //   "args": [ "{{cookiecutter._output_folder_path}}\\readme.txt" ]
                        // }
                        // ]
                        //
                        // Special accommodations for switch arguments:
                        // If the switch takes a value, the switch/value pair should be split in 2 array elements
                        // so the value can be properly quoted independently of the switch, whose name isn't quoted
                        // since space is not allowed in the name. The switch name should end with a colon.
                        //
                        // {
                        //   "name": "File.OpenFile",
                        //   "args": ["c:\\my folder\\my file.txt", "/e:", "Source Code (text) Editor"]
                        // }
                        //

                        ReadCommands(renderedContext, prop);
                    }
                }
            }

            return(renderedContext);
        }
Esempio n. 2
0
        public async Task <TemplateContext> LoadUnrenderedContextAsync(string localTemplateFolder, string userConfigFilePath)
        {
            if (localTemplateFolder == null)
            {
                throw new ArgumentNullException(nameof(localTemplateFolder));
            }

            var unrenderedContext = new TemplateContext();

            var result = await RunGenerateContextScript(_redirector, _envInterpreterPath, localTemplateFolder, userConfigFilePath);

            var contextJson = string.Join(Environment.NewLine, result.StandardOutputLines);
            var context     = (JToken)JObject.Parse(contextJson).SelectToken("cookiecutter");

            if (context != null)
            {
                JProperty vsExtrasProp = null;

                foreach (JProperty prop in context)
                {
                    // Properties that start with underscore are for internal use,
                    // and cookiecutter doesn't prompt for them.
                    if (!prop.Name.StartsWithOrdinal("_"))
                    {
                        if (prop.Value.Type == JTokenType.String ||
                            prop.Value.Type == JTokenType.Integer ||
                            prop.Value.Type == JTokenType.Float)
                        {
                            unrenderedContext.Items.Add(new ContextItem(prop.Name, Selectors.String, prop.Value.ToString()));
                        }
                        else if (prop.Value.Type == JTokenType.Array)
                        {
                            var    elements = new List <string>();
                            JArray ar       = prop.Value as JArray;
                            foreach (JToken element in ar)
                            {
                                elements.Add(element.ToString());
                            }
                            unrenderedContext.Items.Add(new ContextItem(prop.Name, Selectors.List, elements[0], elements.ToArray()));
                        }
                        else
                        {
                            throw new InvalidOperationException(Strings.CookiecutterClient_UnsupportedJsonElementTypeForPorperty.FormatUI(prop.Name));
                        }
                    }
                    else if (prop.Name == "_visual_studio")
                    {
                        vsExtrasProp = prop;
                    }
                    else if (prop.Name == "_visual_studio_post_cmds")
                    {
                        ReadCommands(unrenderedContext, prop);
                    }
                }

                if (vsExtrasProp != null)
                {
                    LoadVisualStudioSpecificContext(unrenderedContext.Items, vsExtrasProp);
                }
            }

            return(unrenderedContext);
        }