Beispiel #1
0
        protected override void Run()
        {
            var progressDialog = new MonoDevelop.Ide.Gui.Dialogs.ProgressDialog(false, true);

            progressDialog.Title = "Finalize Template";
            progressDialog.Show();

            try {
                var solutionFolder = solution.BaseDirectory.ToString();                 //Path.GetDirectoryName (Environment.GetCommandLineArgs()[0]);

                if (!UpdateSolutionFile(solutionFolder))
                {
                    return;
                }

                UpdateCSProjFiles(solutionFolder);
                solution.NeedsReload = true;

                //DeleteTemplateFolder();
                Console.WriteLine("Done.");

                // Display Success message:
                progressDialog.ShowDone(false, false);
                progressDialog.WriteText("Template successfully finalized. Restart Xamarin Studio for changes to take effect.");
                progressDialog.Message = "Finalize Template success. RESTART XAMARIN STUDIO.";
            } catch (Exception ex) {
                Console.WriteLine("ERROR: {0}", ex.Message);

                // Display Error message:
                progressDialog.ShowDone(false, true);
                progressDialog.WriteText(ex.Message);
            }
        }
        protected override async void Run()
        {
            progressDialog         = new MonoDevelop.Ide.Gui.Dialogs.ProgressDialog(false, true);
            Logging.ProgressDialog = progressDialog;
            progressDialog.Title   = "Export Template";
            progressDialog.Message = "Exporting " + solution.Name + "...";

            Logging.Log("0");

            try {
                progressDialog.Progress = 0;
                progressDialog.Show();

                StringBuilder runtimeXml  = new StringBuilder();
                StringBuilder projectsXml = new StringBuilder();

                var templateDir = Path.Combine(rootDir, "ProjectTemplate");

                // Cleanup: Delete template directory if it exists
                if (Directory.Exists(templateDir))
                {
                    Directory.Delete(templateDir, true);
                }

                // Create template directory
                Directory.CreateDirectory(templateDir);

                double progressInterval = 1.0 / projects.Count();
                var    projectNameSpace = GetProjectNameSpace();

                // List of project type guids http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs
                // since XS6 all exposed projecttype guids are general C# guids,
                // but we need the specific project type guids which can be found in the .csproj <ProjectTypeGuids> node.
                foreach (var project in projects)
                {
                    var projType = ((DotNetProject)project).TargetFramework.Id.Identifier;
                    switch (projType)
                    {
                    case "MonoAndroid":
                        projectsXml.Append(Xml.AndroidXml);
                        projectsXml = projectsXml.Replace("[PROJECTTYPE]", "{EFBA0AD7-5A72-4C68-AF49-83D382785DCF}");
                        break;

                    case ".NETPortable":
                        projectsXml.Append(Xml.PclXml);
                        projectsXml = projectsXml.Replace("[PROJECTTYPE]", "{786C830F-07A1-408B-BD7F-6EE04809D6DB}");
                        break;

                    case "Xamarin.iOS":
                        projectsXml.Append(Xml.iOSXml);
                        projectsXml = projectsXml.Replace("[PROJECTTYPE]", "{FEACFBD2-3405-455C-9665-78FE426C6842}");
                        break;

                    case ".NETFramework":
                        projectsXml.Append(Xml.UnitTestXml);
                        projectsXml = projectsXml.Replace("[PROJECTTYPE]", "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}");
                        break;

                    default:
                        continue;
                    }

                    // Set the project name extension so we can have multiple projects of the same type (.iOS, .Android, .Core or whatever is specified in the original project)
                    projectsXml = projectsXml.Replace("[PROJECTNAMEEXTENSION]", project.Name.Replace(projectNameSpace, ""));

                    //proj = (Project)project;
                    await AddOriginalProjectFile((Project)project);
                    await AddOriginalSolutionFile((Project)project);

                    if (!((Project)project).Files.Any())
                    {
                        return;
                    }

                    StringBuilder filesXml      = new StringBuilder();
                    StringBuilder referencesXml = new StringBuilder();
                    string        packagesXml   = "";

                    foreach (var reference in ((MonoDevelop.Projects.DotNetProject)(Project) project).References)
                    {
                        if (reference.ReferenceType != ReferenceType.Assembly)
                        {
                            referencesXml.Append(string.Format("<Reference type=\"{0}\" refto=\"{1}\"/>\n\t\t\t\t", reference.ReferenceType.ToString(), reference.Reference.Replace(solution.Name, "${ProjectName}")));
                        }
                    }

                    runtimeXml.Append("<Import file=\"ProjectTemplate.xpt.xml\" />");

                    int i = -1;
                    foreach (var file in ((Project)project).Files)
                    {
                        if (file.FilePath.ToString().ToLower().EndsWith("packages.config"))
                        {
                            // Get packages from packages.config
                            packagesXml = GetPackages(file);
                            continue;
                        }

                        i++;

                        // Exclude unnecassary files from template
                        if (ExcludeFile(file, i))
                        {
                            continue;
                        }

                        // Copy or Create files from project to ProjectTemplate directory
                        var dir = Path.GetDirectoryName(file.FilePath).Replace(rootDir, templateDir);
                        Directory.CreateDirectory(dir);
                        var templateFilePath = Path.Combine(dir, file.ProjectVirtualPath.FileName);
                        Logging.Log(string.Format("{0} Export Template Copy: {1}", i, file.FilePath));

                        if (file.ProjectVirtualPath.Extension.ToLower() == ".png")
                        {
                            // copy file
                            File.Copy(file.FilePath, templateFilePath, true);
                        }
                        else
                        {
                            // create new file, so we can replace namespaces and projectname
                            var content = File.ReadAllText(file.FilePath);
                            //content = content.Replace(projectNameSpace, "${Namespace}");
                            content = content.Replace(project.Name, "${Namespace}");
                            content = content.Replace(solution.Name, "${SolutionName}");

                            CreateFile(templateFilePath, content, true);
                        }

                        runtimeXml.Append(string.Format("\n\t\t<Import file=\"{0}\" />", solution.GetRelativeChildPath(file.FilePath)));
                        AppendFile(ref filesXml, file, project.Name);
                    }

                    // Replace placeholders
                    projectsXml = projectsXml.Replace("[FILES]", filesXml.ToString());
                    projectsXml = projectsXml.Replace("[PACKAGES]", packagesXml);
                    projectsXml = projectsXml.Replace("[REFERENCES]", referencesXml.ToString());
                    projectsXml = projectsXml.Replace("[DIRECTORY]", project.Name);
                    projectsXml = projectsXml.Replace("[TARGETFRAMEWORK]", ((DotNetProject)project).TargetFramework.Id.ToString());

                    progressDialog.Progress += progressInterval;
                }

                await LoadTemplateFiles();

                // Get xml from template files
                var xptXml   = File.ReadAllText(xptFile);
                var addInXml = File.ReadAllText(addinFile);

                // Get attributes from addin.xml
                var version             = GetAddInAttr("Addin", "version");
                var templateName        = GetAddInAttr("Addin", "name");
                var templateDescription = GetAddInAttr("Addin", "description");
                var category            = GetCategory();

                // Replace placeholders
                addInXml = addInXml.Replace("[PROJECTNAME]", solution.Name);
                addInXml = addInXml.Replace("[RUNTIME]", runtimeXml.ToString());
                xptXml   = xptXml.Replace("[VERSION]", string.Format("v{0}", version));
                xptXml   = xptXml.Replace("[PROJECTS]", projectsXml.ToString());
                xptXml   = xptXml.Replace("[TEMPLATENAME]", templateName);
                xptXml   = xptXml.Replace("[TEMPLATEDESCRIPTION]", templateDescription);
                xptXml   = xptXml.Replace("[CATEGORY]", category);

                // Write template files
                File.WriteAllText(xptFile.Replace(rootDir, templateDir), xptXml);
                File.WriteAllText(addinFile.Replace(rootDir, templateDir), addInXml);

                // create .mpack
                if (!MDTool.Run(templateDir, string.Format("-v setup pack {0}.addin.xml", solution.Name)))
                {
                    // Display Error message:
                    progressDialog.ShowDone(false, true);
                    Logging.Log(string.Format("Export Template ERROR: Unable to generate .mpack"));

                    return;
                }

                var mpack     = string.Format("MonoDevelop.{0}_{1}.mpack", solution.Name, version);
                var mpackPath = Path.Combine(rootDir, mpack);
                File.Copy(Path.Combine(templateDir, mpack), mpackPath, true);

                // install .mpack: INSTALL NOT WORKING :-(
//				if(!RunMDTool(rootDir, string.Format("-v setup install -y {0}", mpack)))
//				{
//					Console.WriteLine("Export Template ERROR: Unable to install .mpack");
//					return;
//				}

                // Display Success message:
                progressDialog.ShowDone(false, false);
                Logging.Log(string.Format("Template successfully exported to: {0}", mpackPath));
                progressDialog.Message = "Export Template SUCCESS: " + mpack;
            }
            catch (Exception ex)
            {
                // Display Error message:
                progressDialog.ShowDone(false, true);

                // Log exception
                Logging.Log(string.Format("Export Template EXCEPTION: {0}", ex.Message));
            }
        }