Example #1
0
        static IEnumerable <string> GetProjectJsonFrameworks(DotNetProject project)
        {
            var packagesJsonFile = project.GetProjectFile(project.BaseDirectory.Combine("project.json"));

            if (packagesJsonFile == null)
            {
                return(null);
            }

            var file = TextFileProvider.Instance.GetEditableTextFile(packagesJsonFile.FilePath.ToString());

            JObject json;

            using (var tr = file.CreateReader())
                using (var jr = new Newtonsoft.Json.JsonTextReader(tr)) {
                    json = (JObject)JToken.Load(jr);
                }

            var frameworks = json ["frameworks"] as JObject;

            if (frameworks == null)
            {
                return(null);
            }

            return(frameworks.Properties().Select(p => p.Name));
        }
        RazorEngineHost CreateRazorHost(string fileName)
        {
            var projectFile = project.GetProjectFile(fileName);

            if (projectFile != null && projectFile.Generator == "RazorTemplatePreprocessor")
            {
                var h = MonoDevelop.RazorGenerator.RazorTemplatePreprocessor.CreateHost(fileName);
                h.DesignTimeMode = true;
                return(h);
            }

            string virtualPath = "~/Views/Default.cshtml";

            if (aspProject != null)
            {
                virtualPath = aspProject.LocalToVirtualPath(fileName);
            }

            WebPageRazorHost host = null;

            // Try to create host using web.config file
            var webConfigMap = new WebConfigurationFileMap();

            if (aspProject != null)
            {
                var vdm = new VirtualDirectoryMapping(aspProject.BaseDirectory.Combine("Views"), true, "web.config");
                webConfigMap.VirtualDirectories.Add("/", vdm);
            }
            Configuration configuration;

            try {
                configuration = WebConfigurationManager.OpenMappedWebConfiguration(webConfigMap, "/");
            } catch {
                configuration = null;
            }
            if (configuration != null)
            {
                var rws = configuration.GetSectionGroup(RazorWebSectionGroup.GroupName) as RazorWebSectionGroup;
                if (rws != null)
                {
                    host = WebRazorHostFactory.CreateHostFromConfig(rws, virtualPath, fileName);
                    host.DesignTimeMode = true;
                }
            }

            if (host == null)
            {
                host = new MvcWebPageRazorHost(virtualPath, fileName)
                {
                    DesignTimeMode = true
                };
                // Add default namespaces from Razor section
                host.NamespaceImports.Add("System.Web.Mvc");
                host.NamespaceImports.Add("System.Web.Mvc.Ajax");
                host.NamespaceImports.Add("System.Web.Mvc.Html");
                host.NamespaceImports.Add("System.Web.Routing");
            }

            return(host);
        }
Example #3
0
        public MD.ProjectFile GetParentFileProjectItem(string fileName)
        {
            string parentFileName = GetParentFileName(fileName);

            if (parentFileName != null)
            {
                return(project.GetProjectFile(parentFileName));
            }
            return(null);
        }
        //TODO error handling
        public void Store()
        {
            bool needsRestore = false;

            //get the new framework and netstandard version
            var isNetStandard = netstandardRadio.Active;
            var nsVersion     = isNetStandard ? NetStandardVersion : null;
            var fx            = TargetFramework;


            //netstandard always uses PCL5 framework
            if (isNetStandard)
            {
                fx = Runtime.SystemAssemblyService.GetTargetFramework(pcl5Tfm);
            }

            //netstandard always uses project.json, ensure it exists
            var projectJsonFile = project.GetProjectFile(project.BaseDirectory.Combine("project.json"));

            if (isNetStandard && projectJsonFile == null)
            {
                projectJsonFile = MigrateToProjectJson(project);
                needsRestore    = true;
            }

            //if project.json exists, update it
            if (projectJsonFile != null)
            {
                var  nugetFx = nsVersion ?? GetPclProfileFullName(fx.Id) ?? NetStandardDefaultFramework;
                bool projectJsonChanged;
                SetProjectJsonValues(projectJsonFile.FilePath, nugetFx, out projectJsonChanged);
                needsRestore = projectJsonChanged;
            }

            //if the framework has changed, update it
            if (fx != null && fx != project.TargetFramework)
            {
                project.TargetFramework = fx;
            }

            if (needsRestore)
            {
                FileService.NotifyFileChanged(projectJsonFile.FilePath);
            }
        }
Example #5
0
 public void CheckGtkFolder()
 {
     foreach (string designerFile in GetDesignerFiles())
     {
         string componentFile = GetComponentFileFromDesigner(designerFile);
         if (componentFile != null)
         {
             string buildFile = GetBuildFileFromComponent(componentFile);
             if (buildFile != null)
             {
                 // if build file is missing, force to generate it again
                 if ((project.GetProjectFile(buildFile) == null) || !File.Exists(buildFile))
                 {
                     FileInfo fi = new FileInfo(designerFile);
                     fi.LastWriteTime = DateTime.Now;
                 }
             }
         }
     }
 }
        void AsyncUpdateDesignerFile(object sender, EventArgs e)
        {
            if (project == null)
            {
                return;
            }

            var file = project.GetProjectFile(FileName);

            if (file == null)
            {
                return;
            }

            var designerFile = WebFormsCodeBehind.GetDesignerFile(file);

            if (designerFile == null)
            {
                return;
            }

            System.Threading.ThreadPool.QueueUserWorkItem(r => {
                using (var monitor = MonoDevelop.Ide.IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor(
                           GettextCatalog.GetString("Updating ASP.NET Designer File..."), null, false))
                {
                    var writer = CodeBehindWriter.CreateForProject(monitor, project);
                    var result = WebFormsCodeBehind.UpdateDesignerFile(writer, project, file, designerFile);
                    //don't worry about reporting error's here for now
                    //if the user wants to see errors, they can compile
                    if (!result.Failed)
                    {
                        writer.WriteOpenFiles();
                    }
                }
            });
        }
Example #7
0
        internal static ProjectFile MigrateToProjectJson(DotNetProject project)
        {
            var projectJsonName = project.BaseDirectory.Combine("project.json");
            var projectJsonFile = new ProjectFile(projectJsonName, BuildAction.None);

            bool          isOpen = false;
            JObject       json;
            ITextDocument file;

            if (System.IO.File.Exists(projectJsonName))
            {
                file = TextFileProvider.Instance.GetTextEditorData(projectJsonFile.FilePath.ToString(), out isOpen);
                using (var tr = file.CreateReader())
                    using (var jr = new Newtonsoft.Json.JsonTextReader(tr)) {
                        json = (JObject)JToken.Load(jr);
                    }
            }
            else
            {
                file          = TextEditorFactory.CreateNewDocument();
                file.FileName = projectJsonName;
                file.Encoding = Encoding.UTF8;
                json          = new JObject(
                    new JProperty("dependencies", new JObject()),
                    new JProperty("frameworks", new JObject())
                    );
            }

            List <string> packages           = null;
            var           packagesConfigFile = project.GetProjectFile(project.BaseDirectory.Combine("packages.config"));

            if (packagesConfigFile != null)
            {
                //NOTE: it might also be open and unsaved, but that's an unimportant edge case, ignore it
                var configDoc = System.Xml.Linq.XDocument.Load(packagesConfigFile.FilePath);
                if (configDoc.Root != null)
                {
                    var deps = (json ["dependencies"] as JObject) ?? ((JObject)(json ["dependencies"] = new JObject()));
                    foreach (var packagelEl in configDoc.Root.Elements("package"))
                    {
                        var packageId      = (string)packagelEl.Attribute("id");
                        var packageVersion = (string)packagelEl.Attribute("version");
                        deps [packageId] = packageVersion;

                        if (packages == null)
                        {
                            packages = new List <string> ();
                        }
                        packages.Add(packageId + "." + packageVersion);
                    }
                }
            }

            var framework = GetPclProfileFullName(project.TargetFramework.Id) ?? NetStandardDefaultFramework;

            json ["frameworks"] = new JObject(
                new JProperty(framework, new JObject())
                );

            file.Text = json.ToString();

            if (!isOpen)
            {
                file.Save();
            }

            project.AddFile(projectJsonFile);
            if (packagesConfigFile != null)
            {
                project.Files.Remove(packagesConfigFile);

                //we have to delete the packages.config, or the NuGet addin will try to retarget its packages
                FileService.DeleteFile(packagesConfigFile.FilePath);

                //remove the package refs nuget put in the file, project.json doesn't use those
                project.References.RemoveRange(project.References.Where(IsFromPackage).ToArray());

                // Remove any imports from NuGet packages. These will be added by NuGet into the generated
                // ProjectName.nuget.props and ProjectName.nuget.targets files when using project.json.
                if (packages != null)
                {
                    foreach (var import in project.MSBuildProject.Imports.ToArray())
                    {
                        if (packages.Any(p => import.Project.IndexOf(p, StringComparison.OrdinalIgnoreCase) >= 0))
                        {
                            import.ParentObject.ParentProject.Remove(import);
                        }
                    }
                }
            }

            return(projectJsonFile);
        }