Example #1
0
        string GetProxyFileNameExtension(string language)
        {
            LanguageBindingDescriptor binding = LanguageBindingService.GetCodonPerLanguageName(language);

            if (binding != null)
            {
                string[] extensions = binding.CodeFileExtensions;
                if (extensions.Length > 0)
                {
                    return(extensions[0]);
                }
            }
            throw new NotSupportedException("Unsupported language: " + language);
        }
Example #2
0
        protected virtual IProject CreateProject(string targetProjectDirectory, IProject sourceProject)
        {
            ProjectCreateInformation info = new ProjectCreateInformation();

            info.Solution        = sourceProject.ParentSolution;
            info.ProjectBasePath = targetProjectDirectory;
            info.ProjectName     = sourceProject.Name + ".Converted";
            info.RootNamespace   = sourceProject.RootNamespace;

            LanguageBindingDescriptor descriptor = LanguageBindingService.GetCodonPerLanguageName(TargetLanguageName);

            if (descriptor == null || descriptor.Binding == null)
            {
                throw new InvalidOperationException("Cannot get Language Binding for " + TargetLanguageName);
            }

            info.OutputProjectFileName = FileUtility.NormalizePath(Path.Combine(targetProjectDirectory, info.ProjectName + descriptor.ProjectFileExtension));

            return(descriptor.Binding.CreateProject(info));
        }
 int GetFileFilterIndex(IProject project, string[] fileFilters)
 {
     if (project != null)
     {
         LanguageBindingDescriptor languageCodon = LanguageBindingService.GetCodonPerLanguageName(project.Language);
         if (languageCodon != null)
         {
             for (int i = 0; i < fileFilters.Length; ++i)
             {
                 for (int j = 0; j < languageCodon.CodeFileExtensions.Length; ++j)
                 {
                     if (fileFilters[i].ToUpperInvariant().IndexOf(languageCodon.CodeFileExtensions[j].ToUpperInvariant()) >= 0)
                     {
                         return(i + 1);
                     }
                 }
             }
         }
     }
     return(0);
 }
        public IProject CreateProject(ProjectCreateInformation projectCreateInformation, string defaultLanguage)
        {
            // remember old outerProjectBasePath
            string outerProjectBasePath = projectCreateInformation.ProjectBasePath;
            string outerProjectName     = projectCreateInformation.ProjectName;

            try
            {
                projectCreateInformation.ProjectBasePath = Path.Combine(projectCreateInformation.ProjectBasePath, this.relativePath);
                if (!Directory.Exists(projectCreateInformation.ProjectBasePath))
                {
                    Directory.CreateDirectory(projectCreateInformation.ProjectBasePath);
                }

                string language = string.IsNullOrEmpty(languageName) ? defaultLanguage : languageName;
                LanguageBindingDescriptor descriptor   = LanguageBindingService.GetCodonPerLanguageName(language);
                ILanguageBinding          languageinfo = (descriptor != null) ? descriptor.Binding : null;

                if (languageinfo == null)
                {
                    StringParser.Properties["type"] = language;
                    MessageService.ShowError("${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.CantCreateProjectWithTypeError}");
                    return(null);
                }

                string newProjectName = StringParser.Parse(name, new string[, ] {
                    { "ProjectName", projectCreateInformation.ProjectName }
                });
                string projectLocation = Path.GetFullPath(Path.Combine(projectCreateInformation.ProjectBasePath,
                                                                       newProjectName + LanguageBindingService.GetProjectFileExtension(language)));


                StringBuilder standardNamespace = new StringBuilder();
                // filter 'illegal' chars from standard namespace
                if (newProjectName != null && newProjectName.Length > 0)
                {
                    char ch = '.';
                    for (int i = 0; i < newProjectName.Length; ++i)
                    {
                        if (ch == '.')
                        {
                            // at beginning or after '.', only a letter or '_' is allowed
                            ch = newProjectName[i];
                            if (!Char.IsLetter(ch))
                            {
                                standardNamespace.Append('_');
                            }
                            else
                            {
                                standardNamespace.Append(ch);
                            }
                        }
                        else
                        {
                            ch = newProjectName[i];
                            // can only contain letters, digits or '_'
                            if (!Char.IsLetterOrDigit(ch) && ch != '.')
                            {
                                standardNamespace.Append('_');
                            }
                            else
                            {
                                standardNamespace.Append(ch);
                            }
                        }
                    }
                }

                projectCreateInformation.OutputProjectFileName = projectLocation;
                projectCreateInformation.RootNamespace         = standardNamespace.ToString();
                projectCreateInformation.ProjectName           = newProjectName;

                StringParser.Properties["StandardNamespace"] = projectCreateInformation.RootNamespace;

                IProject project;
                try {
                    project = languageinfo.CreateProject(projectCreateInformation);
                } catch (ProjectLoadException ex) {
                    MessageService.ShowError(ex.Message);
                    return(null);
                }

                // Add Project items
                foreach (ProjectItem projectItem in projectItems)
                {
                    ProjectItem newProjectItem = new UnknownProjectItem(
                        project,
                        StringParser.Parse(projectItem.ItemType.ItemName),
                        StringParser.Parse(projectItem.Include)
                        );
                    foreach (string metadataName in projectItem.MetadataNames)
                    {
                        string metadataValue = projectItem.GetMetadata(metadataName);
                        // if the input contains any special MSBuild sequences, don't escape the value
                        // we want to escape only when the special characters are introduced by the StringParser.Parse replacement
                        if (metadataValue.Contains("$(") || metadataValue.Contains("%"))
                        {
                            newProjectItem.SetMetadata(StringParser.Parse(metadataName), StringParser.Parse(metadataValue));
                        }
                        else
                        {
                            newProjectItem.SetEvaluatedMetadata(StringParser.Parse(metadataName), StringParser.Parse(metadataValue));
                        }
                    }
                    ((IProjectItemListProvider)project).AddProjectItem(newProjectItem);
                }

                // Add Imports
                if (clearExistingImports || projectImports.Count > 0)
                {
                    if (!(project is MSBuildBasedProject))
                    {
                        throw new Exception("<Imports> may be only used in project templates for MSBuildBasedProjects");
                    }

                    if (clearExistingImports)
                    {
                        MSBuildInternals.ClearImports(((MSBuildBasedProject)project).MSBuildProject);
                    }
                    try {
                        foreach (Import projectImport in projectImports)
                        {
                            ((MSBuildBasedProject)project).MSBuildProject.AddNewImport(projectImport.Key, projectImport.Value);
                        }
                        ((MSBuildBasedProject)project).CreateItemsListFromMSBuild();
                    } catch (MSBuild.InvalidProjectFileException ex) {
                        if (string.IsNullOrEmpty(importsFailureMessage))
                        {
                            MessageService.ShowError("Error creating project:\n" + ex.Message);
                        }
                        else
                        {
                            MessageService.ShowError(importsFailureMessage + "\n\n" + ex.Message);
                        }
                        return(null);
                    }
                }

                if (projectProperties.Count > 0)
                {
                    if (!(project is MSBuildBasedProject))
                    {
                        throw new Exception("<PropertyGroup> may be only used in project templates for MSBuildBasedProjects");
                    }

                    foreach (ProjectProperty p in projectProperties)
                    {
                        ((MSBuildBasedProject)project).SetProperty(
                            StringParser.Parse(p.Configuration),
                            StringParser.Parse(p.Platform),
                            StringParser.Parse(p.Name),
                            StringParser.Parse(p.Value),
                            p.Location,
                            p.ValueIsLiteral
                            );
                    }
                }

                // Add Files
                foreach (FileDescriptionTemplate file in files)
                {
                    string fileName = Path.Combine(projectCreateInformation.ProjectBasePath, StringParser.Parse(file.Name, new string[, ] {
                        { "ProjectName", projectCreateInformation.ProjectName }
                    }));
                    FileProjectItem projectFile = new FileProjectItem(project, project.GetDefaultItemType(fileName));

                    projectFile.Include = FileUtility.GetRelativePath(project.Directory, fileName);

                    file.SetProjectItemProperties(projectFile);

                    ((IProjectItemListProvider)project).AddProjectItem(projectFile);

                    if (File.Exists(fileName))
                    {
                        StringParser.Properties["fileName"] = fileName;
                        if (!MessageService.AskQuestion("${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion}", "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion.InfoName}"))
                        {
                            continue;
                        }
                    }

                    try {
                        if (!Directory.Exists(Path.GetDirectoryName(fileName)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                        }
                        if (file.ContentData != null)
                        {
                            // Binary content
                            File.WriteAllBytes(fileName, file.ContentData);
                        }
                        else
                        {
                            // Textual content
                            StreamWriter sr          = new StreamWriter(File.Create(fileName), ParserService.DefaultFileEncoding);
                            string       fileContent = StringParser.Parse(file.Content, new string[, ] {
                                { "ProjectName", projectCreateInformation.ProjectName }, { "FileName", fileName }
                            });
                            fileContent = StringParser.Parse(fileContent);
                            if (SharpDevelopTextEditorProperties.Instance.IndentationString != "\t")
                            {
                                fileContent = fileContent.Replace("\t", SharpDevelopTextEditorProperties.Instance.IndentationString);
                            }
                            sr.Write(fileContent);
                            sr.Close();
                        }
                    } catch (Exception ex) {
                        StringParser.Properties["fileName"] = fileName;
                        MessageService.ShowError(ex, "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.FileCouldntBeWrittenError}");
                    }
                }

                RunCreateActions(project);

                // Save project
                if (File.Exists(projectLocation))
                {
                    StringParser.Properties["projectLocation"] = projectLocation;
                    if (MessageService.AskQuestion("${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteProjectQuestion}", "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion.InfoName}"))
                    {
                        project.Save();
                    }
                }
                else
                {
                    project.Save();
                }

                projectCreateInformation.createdProjects.Add(project);
                ProjectService.OnProjectCreated(new ProjectEventArgs(project));
                return(project);
            }
            finally
            {
                // set back outerProjectBasePath
                projectCreateInformation.ProjectBasePath = outerProjectBasePath;
                projectCreateInformation.ProjectName     = outerProjectName;
            }
        }