示例#1
0
 public ThemeWindow(ThemeViewModel viewModel)
 {
     InitializeComponent();
     DataContext = viewModel;
 }
        /// <summary>
        /// Fired when a user selects the create a new theme menu option
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ThemeCallback(object sender, EventArgs e)
        {
            var ssol = dte.Solution;
            var projects = ssol.Projects;
            //if we need to use codegen or can use our extended theme generator
            bool extended = CheckFramework(projects);

            var vm = new ThemeViewModel();
            vm.Codegen = !extended;
            var window = new ThemeWindow(vm);
            var success = window.ShowDialog();

            if (String.IsNullOrWhiteSpace(vm.ThemeName))
            {
                FireError("Please specify a name for your theme!");
                return;
            }

            vm.ThemeName = vm.ThemeName.Replace(" ", String.Empty);
            vm.Version = vm.Version ?? vm.Versions[0];

            if (success.GetValueOrDefault() == false)
                return;

            var solution = GetGlobalService(typeof(SVsSolution)) as IVsSolution;

            //if (!extended)
            if(false)
            {
                var path = GetOrchardExe(solution);
                if (!File.Exists(path))
                {
                    FireError("Cannot find Orchard.exe, try building the solution and then creating your theme again");
                    return;
                }

                var basedon = String.IsNullOrEmpty(vm.BasedOn) ? String.Empty : "/BasedOn:" + vm.BasedOn.Trim();
                var cproj = vm.CreateProject ? "/CreateProject:true" : String.Empty;

                var args = String.Format("codegen theme {0} {1} {2} /IncludeInSolution:true",
                    Regex.Replace(vm.ThemeName, @"\s+", ""), cproj, basedon);

                ProcessStartInfo start = new ProcessStartInfo
                {
                    FileName = path,
                    Arguments = "feature enable Orchard.CodeGeneration"
                };
                Process.Start(start).WaitForExit();

                ProcessStartInfo start2 = new ProcessStartInfo { FileName = path, Arguments = args };
                Process.Start(start2);
                return;
            }

            if (vm.Type == null) vm.Type = "Blank";

            // get the Themes folder in the solution
            Project themesFolderProject = (from Project p in projects where p.Name == "Themes" select p).FirstOrDefault();
            if (themesFolderProject == null)
            {
                FireError("There appears to be no Themes folder");
                return;
            }
            SolutionFolder themesFolder = themesFolderProject.Object as SolutionFolder;

            // Orchard templates
            var templates = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "OrchardTemplates");
            Project theme = (from ProjectItem item in themesFolderProject.ProjectItems where item.Name == "Themes" select item.Object as Project).FirstOrDefault();

            //if (vm.Type.Contains("Bootstrap"))
            //{
            //    FireError("I haven't created the bootstrap theme... but I will! Maybe. Probably not. I'm lazy");
            //    return;
            //}

                if (vm.CreateProject)
                {
                    BuildThemeWithProject(themesFolder, vm, templates, "__BlankThemeProject");
                    return;
                }

                BuildThemeFromTemplate(theme, vm, templates, "__BlankTheme");
                return;

            //if (vm.Type.Contains("Theme Machine"))
            //{
            //    if (vm.CreateProject)
            //    {
            //        var themeType = vm.Responsive ? "__TMRP" : "__TMP";
            //        BuildThemeWithProject(themesFolder, vm, templates, themeType);
            //        return;
            //    }
            //    else
            //    {
            //        var themeType = vm.Responsive ? "__TMR" : "__TM";
            //        BuildThemeFromTemplate(theme, vm, templates, themeType);
            //        return;
            //    }
            //}

            //// get the themes project
            //if (theme == null)
            //    FireError("Could not find themes folder!");

            //var projItems = theme.ProjectItems;
            //var themePath = theme.FileName.Replace("Themes.csproj", vm.ThemeName);

            //var newproj = projItems.AddFromDirectory(templates + "\\BlankTheme");
            //newproj.Name = vm.ThemeName;
            //Insert(themePath + "\\Theme.txt", new[]
            //{
            //    new KeyValuePair<string, string>("$ThemeName$", vm.ThemeName),
            //    new KeyValuePair<string, string>("$Author$", vm.Author ?? "Hazzamanic"),
            //    new KeyValuePair<string, string>("$Description$", vm.Description ?? "Theme created by Orchardizer"),
            //    new KeyValuePair<string, string>("$BasedOn$", vm.BasedOn ?? "")
            //});

            //if (vm.IncludeHelpFile)
            //    newproj.ProjectItems.AddFromFile(templates + "\\ThemeHelp.md");
        }
        /// <summary>
        /// Builds a blank theme including a project file.
        /// </summary>
        /// <param name="themesFolder">The themes folder.</param>
        /// <param name="vm">The vm.</param>
        /// <param name="templates">The templates.</param>
        /// <param name="themeType">Type of theme.</param>
        private void BuildThemeWithProject(SolutionFolder themesFolder, ThemeViewModel vm, string templates, string themeType)
        {
            var solution = GetGlobalService(typeof(SVsSolution)) as IVsSolution;
            var envsol = dte.Solution;

            string solpath;
            string solfile;
            string soloptions;
            solution.GetSolutionInfo(out solpath, out solfile, out soloptions);

            var newDir = Path.Combine(solpath, "Orchard.Web", "Themes", vm.ThemeName);
            var newproj = themesFolder.AddFromTemplate(Path.Combine(templates, themeType, vm.Version, themeType + ".csproj"), newDir, vm.ThemeName);

            Insert(Path.Combine(newDir, vm.ThemeName + ".csproj"),
                new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("$$ModuleProjectGuid$$", Guid.NewGuid().ToString()),
                    new KeyValuePair<string, string>("$$ModuleName$$", vm.ThemeName)
                });
            // this does work...
            newproj.Properties.Item("AssemblyName").Value = vm.ThemeName;
            // need this as well because... I have no idea
            newproj.Properties.Item("RootNamespace").Value = vm.ThemeName;
            EditThemeFile(newDir, vm);

            // isn't added to tfs for some reason...
            if (vm.IncludeHelpFile)
                newproj.ProjectItems.AddFromFile(templates + "\\ThemeHelp.md");

            newproj.Save();
        }
 /// <summary>
 /// Edits the theme file.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="vm">The vm.</param>
 private void EditThemeFile(string path, ThemeViewModel vm)
 {
     Insert(path + "\\Theme.txt", new[]
     {
         new KeyValuePair<string, string>("$ThemeName$", vm.ThemeName),
         new KeyValuePair<string, string>("$Author$", vm.Author ?? "Orchardizer"),
         new KeyValuePair<string, string>("$Description$", vm.Description ?? "Theme created by Orchardizer"),
         new KeyValuePair<string, string>("$BasedOn$", String.IsNullOrWhiteSpace(vm.BasedOn) ? "" : "BaseTheme: " + vm.BasedOn)
     });
 }
        /// <summary>
        /// Builds a blank theme, no project file
        /// </summary>
        /// <param name="theme">The theme.</param>
        /// <param name="vm">The vm.</param>
        /// <param name="templates">The templates.</param>
        /// <param name="themeType">Type of theme.</param>
        private void BuildThemeFromTemplate(Project theme, ThemeViewModel vm, string templates, string themeType)
        {
            var projItems = theme.ProjectItems;
            var themePath = theme.FileName.Replace("Themes.csproj", vm.ThemeName);

            var newproj = projItems.AddFromDirectory(Path.Combine(templates, vm.Version, themeType));
            newproj.Name = vm.ThemeName;
            EditThemeFile(themePath, vm);

            if (vm.IncludeHelpFile)
                newproj.ProjectItems.AddFromFile(templates + "\\ThemeHelp.md");

            newproj.Save();
        }