public override Task <bool> AddToProjectAsync(SolutionFolderItem policyParent, Project project, string language, string directory, string name)
        {
            var model    = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, name, null);
            var fileName = StringParserService.Parse(name, model);

            project.ProjectProperties.SetValue(typeAtt.Value, string.IsNullOrEmpty(fileName) ? propertyInnerText : string.Concat(fileName, extension));
            return(Task.FromResult(true));
        }
        public async Task <ProjectFile> AddFileToProject(SolutionFolderItem policyParent, Project project, string language, string directory, string name)
        {
            generatedFile = await SaveFile(policyParent, project, language, directory, name);

            if (generatedFile != null)
            {
                string      buildAction = this.buildAction ?? project.GetDefaultBuildAction(generatedFile);
                ProjectFile projectFile = project.AddFile(generatedFile, buildAction);

                if (!string.IsNullOrEmpty(dependsOn))
                {
                    var    model         = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, name, generatedFile);
                    string parsedDepName = StringParserService.Parse(dependsOn, model);
                    if (projectFile.DependsOn != parsedDepName)
                    {
                        projectFile.DependsOn = parsedDepName;
                    }
                }

                if (!string.IsNullOrEmpty(customTool))
                {
                    projectFile.Generator = customTool;
                }

                if (!string.IsNullOrEmpty(customToolNamespace))
                {
                    var model = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, name, generatedFile);
                    projectFile.CustomToolNamespace = StringParserService.Parse(customToolNamespace, model);
                }

                if (!string.IsNullOrEmpty(subType))
                {
                    projectFile.ContentType = subType;
                }

                DotNetProject netProject = project as DotNetProject;
                if (netProject != null)
                {
                    // Add required references
                    foreach (string aref in references)
                    {
                        string res = netProject.AssemblyContext.GetAssemblyFullName(aref, netProject.TargetFramework);
                        res = netProject.AssemblyContext.GetAssemblyNameForVersion(res, netProject.TargetFramework);
                        if (!ContainsReference(netProject, res))
                        {
                            netProject.References.Add(ProjectReference.CreateAssemblyReference(aref));
                        }
                    }
                }

                return(projectFile);
            }
            else
            {
                return(null);
            }
        }
        CombinedTagModel GetTagModel(SolutionItem policyParent, Project project, string language, string identifier, string fileName)
        {
            var model = new CombinedTagModel {
                BaseModel = ProjectTagModel
            };

            ModifyTags(policyParent, project, language, identifier, fileName, ref model.OverrideTags);
            return(model);
        }
Example #4
0
        public static CombinedTagModel GetTagModel(IStringTagModel projectTagModel, SolutionFolderItem policyParent, Project project, string language, string identifier, string fileName)
        {
            var model = new CombinedTagModel {
                BaseModel = projectTagModel
            };

            FileTemplateTagsModifier.ModifyTags(policyParent, project, language, identifier, fileName, ref model.OverrideTags);
            return(model);
        }
Example #5
0
        // Returns the name of the file that this template generates.
        // All parameters are optional (can be null)
        public virtual string GetFileName(SolutionFolderItem policyParent, Project project, string language, string baseDirectory, string entryName)
        {
            if (string.IsNullOrEmpty(entryName) && !string.IsNullOrEmpty(defaultName))
            {
                entryName = defaultName;
            }

            string fileName = entryName;

            //substitute tags
            if ((name != null) && (name.Length > 0))
            {
                //checks if the entryName contains the extension (i.e. added explicitly with the name)
                var identifier = entryName ?? name;
                if (identifier.EndsWith(Path.GetExtension(name), StringComparison.OrdinalIgnoreCase))
                {
                    identifier = Path.GetFileNameWithoutExtension(identifier);
                }
                var model = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, identifier, fileName);
                fileName = StringParserService.Parse(name, model);
            }

            if (fileName == null)
            {
                throw new InvalidOperationException(GettextCatalog.GetString("File name not provided in template"));
            }

            //give it an extension if it didn't get one (compatibility with pre-substition behaviour)
            if (Path.GetExtension(fileName).Length == 0)
            {
                if (defaultExtensionDefined)
                {
                    fileName = fileName + defaultExtension;
                }
                else if (!string.IsNullOrEmpty(language))
                {
                    var languageBinding = GetLanguageBinding(language);
                    fileName = languageBinding.GetFileName(fileName);
                }
            }

            if (baseDirectory != null)
            {
                fileName = Path.Combine(baseDirectory, fileName);
            }

            return(fileName);
        }
Example #6
0
        // Returns the name of the file that this template generates.
        // All parameters are optional (can be null)
        public virtual string GetFileName(SolutionFolderItem policyParent, Project project, string language, string baseDirectory, string entryName)
        {
            if (string.IsNullOrEmpty(entryName) && !string.IsNullOrEmpty(defaultName))
            {
                entryName = defaultName;
            }

            string fileName = entryName;

            //substitute tags
            if ((name != null) && (name.Length > 0))
            {
                var model = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, entryName ?? name, null);
                fileName = StringParserService.Parse(name, model);
            }

            if (fileName == null)
            {
                throw new InvalidOperationException("File name not provided in template");
            }

            //give it an extension if it didn't get one (compatibility with pre-substition behaviour)
            if (Path.GetExtension(fileName).Length == 0)
            {
                if (defaultExtensionDefined)
                {
                    fileName = fileName + defaultExtension;
                }
                else if (!string.IsNullOrEmpty(language))
                {
                    var languageBinding = GetLanguageBinding(language);
                    fileName = languageBinding.GetFileName(fileName);
                }
            }

            if (baseDirectory != null)
            {
                fileName = Path.Combine(baseDirectory, fileName);
            }

            return(fileName);
        }
        // Returns a stream with the content of the file.
        // project and language parameters are optional
        public virtual Stream CreateFileContent(SolutionFolderItem policyParent, Project project, string language, string fileName, string identifier)
        {
            var model = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, identifier, fileName);

            //HACK: for API compat, CreateContent just gets the override, not the base model
            // but ProcessContent gets the entire model
            string content = CreateContent(project, model.OverrideTags, language);

            content = ProcessContent(content, model);

            string mime      = DesktopService.GetMimeTypeForUri(fileName);
            var    formatter = !string.IsNullOrEmpty(mime) ? CodeFormatterService.GetFormatter(mime) : null;

            if (formatter != null)
            {
                var formatted = formatter.FormatText(policyParent != null ? policyParent.Policies : null, content);
                if (formatted != null)
                {
                    content = formatted;
                }
            }

            var ms = new MemoryStream();

            var bom = Encoding.UTF8.GetPreamble();

            ms.Write(bom, 0, bom.Length);

            byte[] data;
            if (AddStandardHeader)
            {
                string header = StandardHeaderService.GetHeader(policyParent, fileName, true);
                data = System.Text.Encoding.UTF8.GetBytes(header);
                ms.Write(data, 0, data.Length);
            }

            var doc = TextEditorFactory.CreateNewDocument();

            doc.Text = content;

            TextStylePolicy textPolicy = policyParent != null?policyParent.Policies.Get <TextStylePolicy> ("text/plain")
                                             : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> ("text/plain");

            string eolMarker = TextStylePolicy.GetEolMarker(textPolicy.EolMarker);

            byte[] eolMarkerBytes = System.Text.Encoding.UTF8.GetBytes(eolMarker);

            var tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null;

            foreach (var line in doc.GetLines())
            {
                var lineText = doc.GetTextAt(line.Offset, line.Length);
                if (tabToSpaces != null)
                {
                    lineText = lineText.Replace("\t", tabToSpaces);
                }
                data = System.Text.Encoding.UTF8.GetBytes(lineText);
                ms.Write(data, 0, data.Length);
                ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length);
            }

            ms.Position = 0;
            return(ms);
        }
        // Returns a stream with the content of the file.
        // project and language parameters are optional
        public virtual async Task <Stream> CreateFileContentAsync(SolutionFolderItem policyParent, Project project, string language, string fileName, string identifier)
        {
            var model = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, identifier, fileName);

            //HACK: for API compat, CreateContent just gets the override, not the base model
            // but ProcessContent gets the entire model
            string content = CreateContent(project, model.OverrideTags, language);

            content = ProcessContent(content, model);

            string mime      = DesktopService.GetMimeTypeForUri(fileName);
            var    formatter = !string.IsNullOrEmpty(mime) ? CodeFormatterService.GetFormatter(mime) : null;

            if (formatter != null)
            {
                var formatted = formatter.FormatText(policyParent != null ? policyParent.Policies : null, content);
                if (formatted != null)
                {
                    content = formatted;
                }
            }

            var             ms         = new MemoryStream();
            Encoding        encoding   = null;
            TextStylePolicy textPolicy = policyParent != null?policyParent.Policies.Get <TextStylePolicy> (mime ?? "text/plain")
                                             : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> (mime ?? "text/plain");

            string eolMarker = TextStylePolicy.GetEolMarker(textPolicy.EolMarker);

            var ctx = await EditorConfigService.GetEditorConfigContext(fileName);

            if (ctx != null)
            {
                ctx.CurrentConventions.UniversalConventions.TryGetEncoding(out encoding);
                if (ctx.CurrentConventions.UniversalConventions.TryGetLineEnding(out string lineEnding))
                {
                    eolMarker = lineEnding;
                }
            }
            if (encoding == null)
            {
                encoding = System.Text.Encoding.UTF8;
            }
            var bom = encoding.GetPreamble();

            if (bom != null && bom.Length > 0)
            {
                ms.Write(bom, 0, bom.Length);
            }

            byte[] data;
            if (AddStandardHeader)
            {
                string header = StandardHeaderService.GetHeader(policyParent, fileName, true);
                data = encoding.GetBytes(header);
                ms.Write(data, 0, data.Length);
            }

            var doc = TextEditorFactory.CreateNewDocument();

            doc.Text = content;


            byte[] eolMarkerBytes = encoding.GetBytes(eolMarker);

            var           tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null;
            IDocumentLine lastLine    = null;

            foreach (var line in doc.GetLines())
            {
                var lineText = doc.GetTextAt(line.Offset, line.Length);
                if (tabToSpaces != null)
                {
                    lineText = lineText.Replace("\t", tabToSpaces);
                }
                if (line.LengthIncludingDelimiter > 0)
                {
                    data = encoding.GetBytes(lineText);
                    ms.Write(data, 0, data.Length);
                    ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length);
                }
                lastLine = line;
            }
            if (ctx != null && lastLine != null && lastLine.Length > 0)
            {
                if (ctx.CurrentConventions.UniversalConventions.TryGetRequireFinalNewline(out bool requireNewLine))
                {
                    if (requireNewLine)
                    {
                        ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length);
                    }
                }
            }

            ms.Position = 0;
            return(ms);
        }
		CombinedTagModel GetTagModel (SolutionItem policyParent, Project project, string language, string identifier, string fileName)
		{
			var model = new CombinedTagModel { BaseModel = ProjectTagModel };
			ModifyTags (policyParent, project, language, identifier, fileName, ref model.OverrideTags);
			return model;
		}
		public static CombinedTagModel GetTagModel (IStringTagModel projectTagModel, SolutionFolderItem policyParent, Project project, string language, string identifier, string fileName)
		{
			var model = new CombinedTagModel { BaseModel = projectTagModel };
			FileTemplateTagsModifier.ModifyTags (policyParent, project, language, identifier, fileName, ref model.OverrideTags);
			return model;
		}