示例#1
0
        public static TemplateProperties ParseTemplateJson(string text)
        {
            JsonElement?GetProp(JsonElement root, string name)
            => root.TryGetProperty(name, out var prop) ? (JsonElement?)prop : null;
            string GetPropStr(JsonElement root, string name)
            => root.TryGetProperty(name, out var prop) ? prop.GetString() : null;

            var jd   = JsonDocument.Parse(text);
            var root = jd.RootElement;

            var props = new TemplateProperties();

            props.Name          = GetPropStr(root, "name");
            props.Description   = GetPropStr(root, "description");
            props.Author        = GetPropStr(root, "author");
            props.DefaultName   = GetPropStr(root, "defaultName");
            props.Identity      = GetPropStr(root, "identity");
            props.GroupIdentity = GetPropStr(root, "groupIdentity");
            var tags     = GetProp(root, "tags");
            var language = tags == null ? null : GetPropStr(tags.Value, "language");

            props.LanguageTag = language switch
            {
                "C#" => "csharp",
                "F#" => "fsharp",
                "VB" => "visualbasic",
                _ => "csharp"
            };

            return(props);
        }
    }
示例#2
0
        private static VSTemplate CreateVSTemplate(bool force, TemplateProperties props)
        {
            var tmpl = new VSTemplate();

            tmpl.Version = "3.0.0";
            tmpl.Type    = "ProjectGroup";

            var td = tmpl.TemplateData = new TemplateData();

            td.Name        = props.Name;
            td.Description = props.Description;
            td.Icon        = props.IconZipPath;
            td.ProjectType = "CSharp";
            td.DefaultName = props.DefaultName;
            td.LanguageTag = props.LanguageTag;
            if (props.PlatformTags != null)
            {
                foreach (var pt in props.PlatformTags)
                {
                    td.PlatformTag.Add(pt);
                }
            }
            if (props.ProjectTypeTags != null)
            {
                foreach (var pt in props.ProjectTypeTags)
                {
                    td.ProjectTypeTag.Add(pt);
                }
            }

            //td.CreateNewFolder = true;
            //td.ProvideDefaultName = true;
            //td.LocationField = TemplateDataLocationField.Enabled;
            //td.EnableLocationBrowseButton = true;

            var tc = tmpl.TemplateContent = new VSTemplateTemplateContent();

            tc.CustomParameters.Add(new CustomParameter {
                Name = "$language$", Value = "CSharp"
            });
            tc.CustomParameters.Add(new CustomParameter {
                Name = "$uistyle$", Value = "none"
            });
            tc.CustomParameters.Add(new CustomParameter {
                Name = "$groupid$", Value = props.GroupIdentity
            });

            var we = tmpl.WizardExtension = new VSTemplateWizardExtension();

            we.Assembly      = "Microsoft.VisualStudio.TemplateEngine.Wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
            we.FullClassName = "Microsoft.VisualStudio.TemplateEngine.Wizard.TemplateEngineWizard";

            return(tmpl);
        }
示例#3
0
        public static async Task <int> PackVsix(
            string source,
            string vsix,
            bool force,
            string obj,
            string vsixVersion,
            string moreInfo,
            string licenseFile,
            string releaseNotes,
            string packageIcon,
            string previewImg,
            string[] packageTags,
            string gettingStarted,
            string[] templateIcon,
            string[] languageTag,
            string[] platformTags,
            string[] typeTags,
            string[] defaultName)
        {
            var l = new Logger();

            if (source is null)
            {
                l.LogError("No source file given. Pass --help to show help text.");
                return(1);
            }
            if (!File.Exists(source))
            {
                l.LogError($"Source file '{source}' does not exist.");
                return(1);
            }

            l.Log($"Generating VSIX template package for '{source}'.");

            var templateIconMappings = templateIcon?.Select(s => new TemplatePropertyMapping(s));
            var languageTagMappings  = languageTag?.Select(s => new TemplatePropertyMapping(s));
            var platformTagsMappings = platformTags?.Select(s => new TemplatePropertyMapping(s));
            var typeTagsMappings     = typeTags?.Select(s => new TemplatePropertyMapping(s));
            var defaultNameMappings  = defaultName?.Select(s => new TemplatePropertyMapping(s));

            l.Log("Parsing NuGet metadata.");

            var pkg = await NuPackage.Open(source);

            var metadata  = pkg.Metadata;
            var vsixProps = VsixProperties.FromNuspec(metadata);

            var sourceFolder = Path.GetDirectoryName(source);

            vsix = vsix ?? Path.Combine(sourceFolder, vsixProps.Id + ".vsix");

            if (File.Exists(vsix) && !force)
            {
                l.LogError($"File exists at output path '{vsix}'. Set the --force flag to overwrite it.");
                return(1);
            }

            if (vsixVersion != null)
            {
                vsixProps.Version = vsixVersion;
            }
            if (moreInfo != null)
            {
                vsixProps.MoreInfo = moreInfo;
            }
            if (licenseFile != null)
            {
                vsixProps.License = licenseFile;
            }
            if (releaseNotes != null)
            {
                vsixProps.ReleaseNotes = releaseNotes;
            }
            if (packageIcon != null)
            {
                vsixProps.Icon = packageIcon;
            }
            if (previewImg != null)
            {
                vsixProps.PreviewImage = previewImg;
            }
            if (packageTags != null)
            {
                var splitPackageTags = packageTags.SelectMany(ts => ts.Split(new char[0], StringSplitOptions.RemoveEmptyEntries));
                vsixProps.Tags = string.Join(';', splitPackageTags);
            }
            if (gettingStarted != null)
            {
                vsixProps.GettingStartedGuide = gettingStarted;
            }

            var archive           = pkg.Archive;
            var templateJsonFiles = archive.Entries.Where(e => e.FullName.EndsWith(".template.config/template.json"));

            l.Log("Generating .vstemplate files.");

            var templateContexts = new List <TemplateContext>();

            foreach (var templateJsonFile in templateJsonFiles)
            {
                var templateJsonContent = new StreamReader(templateJsonFile.Open()).ReadToEnd();

                // TODO apply props overrides
                var props = TemplateProperties.ParseTemplateJson(templateJsonContent);

                props.Icon = MapTemplateProperties(props.Identity, templateIconMappings)?.FirstOrDefault();
                if (languageTagMappings != null && languageTagMappings.Any())
                {
                    props.LanguageTag = MapTemplateProperties(props.Identity, languageTagMappings)?.FirstOrDefault() ?? props.LanguageTag;
                }
                props.PlatformTags    = MapTemplateProperties(props.Identity, platformTagsMappings);
                props.ProjectTypeTags = MapTemplateProperties(props.Identity, typeTagsMappings);

                props.DefaultName = MapTemplateProperties(props.Identity, defaultNameMappings)?.FirstOrDefault() ?? props.DefaultName;

                var vsTemplate = CreateVSTemplate(true, props);

                var context = new TemplateContext(templateJsonContent, props, vsTemplate);
                templateContexts.Add(context);
            }

            var di = Directory.CreateDirectory(obj);

            // clear obj directory
            foreach (var fi in di.GetFiles())
            {
                fi.Delete();
            }
            foreach (var fi in di.GetDirectories())
            {
                fi.Delete(true);
            }

            l.Log("Creating VSIX project.");
            l.Indent();

            var vsixDir = Path.Combine(obj, "Vsix");

            Directory.CreateDirectory(vsixDir);

            var vsixProject = VsixProject.Create(vsixDir, vsixProps, l);

            var vsixProjectPath = Path.Combine(vsixDir, "Vsix.csproj");

            var sourceFileName = Path.GetFileName(source);

            File.Copy(source, Path.Combine(vsixDir, sourceFileName));
            vsixProject.AddNupkg(sourceFileName);

            foreach (var ctx in templateContexts)
            {
                var zipFileName = ctx.TemplateJsonProps.Identity + ".zip";

                var tmpZipFolder = Path.Combine(obj, "zip", ctx.TemplateJsonProps.Identity);
                Directory.CreateDirectory(tmpZipFolder);

                File.WriteAllText(Path.Combine(tmpZipFolder, "template.json"), ctx.TemplateJsonContent);
                ctx.VSTemplate.Write(Path.Combine(tmpZipFolder, "template.vstemplate"));

                if (ctx.TemplateJsonProps.Icon != null)
                {
                    File.Copy(ctx.TemplateJsonProps.Icon, Path.Combine(tmpZipFolder, ctx.TemplateJsonProps.IconZipPath));
                }

                ZipFile.CreateFromDirectory(tmpZipFolder, Path.Combine(vsixDir, zipFileName));
                vsixProject.AddTemplateZip(zipFileName, Path.Combine("Templates", ctx.TemplateJsonProps.Identity));

                l.Log($"Added template '{ctx.TemplateJsonProps.Identity}'.");
            }

            vsixProject.Write(vsixProjectPath);

            l.Dedent();

            if (!BuildVsix(l, obj, vsixProjectPath))
            {
                l.LogError("Failed to build vsix.");
                return(1);
            }

            var builtVsixPath = Path.Combine(vsixDir, "bin", "Vsix.vsix");
            var outputVsixDir = Path.GetDirectoryName(vsix);

            if (!Directory.Exists(outputVsixDir))
            {
                Directory.CreateDirectory(outputVsixDir);
            }

            File.Copy(builtVsixPath, vsix, force);

            l.Log($"VSIX generated at {Path.GetFullPath(vsix)}.");

            return(0);
        }