예제 #1
0
        /// <summary>
        /// Add a "baseHref" property in .angular-cli.json.
        /// </summary>
        /// <param name="projectDirectory"></param>
        /// <returns></returns>
        private bool ModifyAngularCliJsonFile(string projectDirectory)
        {
            const string BaseHrefPropertyName = "baseHref";
            const string BaseHrefApi          = "/";
            const string BaseHrefMvc          = "/ng/";

            var projectIsMvc          = Directory.Exists(Path.Combine(projectDirectory, "Views"));
            var projectIsRazorPages   = Directory.Exists(Path.Combine(projectDirectory, "Pages"));
            var baseHrefPropertyValue = (projectIsMvc || projectIsRazorPages) ? BaseHrefMvc : BaseHrefApi;

            var filePath = Path.Combine(projectDirectory, NgWizardHelper.AngularCliJsonFileName);

            if (File.Exists(filePath))
            {
                var rootObj = JObject.Parse(File.ReadAllText(filePath));
                var apps    = (JArray)rootObj["apps"];
                if ((apps != null) && apps.Any())
                {
                    var app = (JObject)apps[0];
                    if (app[BaseHrefPropertyName] == null)
                    {
                        var knownProperty = app.Property("outDir") ?? app.Property("root");
                        if (knownProperty != null)
                        {
                            var baseHrefProperty = new JProperty(BaseHrefPropertyName, baseHrefPropertyValue);
                            knownProperty.AddAfterSelf(baseHrefProperty);

                            NgWizardHelper.RewriteFile(filePath, rootObj.ToString());
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Add a "baseHref" property in angular.json (ver.6)
        /// </summary>
        /// <param name="projectDirectory"></param>
        /// <param name="jsonFileName"></param>
        /// <param name="projectName"></param>
        /// <returns></returns>
        private bool ModifyAngularJsonFile(string projectDirectory, string jsonFileName, string projectName)
        {
            const string BaseHrefPropertyName = "baseHref";
            const string BaseHrefApi          = "/";
            const string BaseHrefMvc          = "/ng/";

            var projectIsMvc          = Directory.Exists(Path.Combine(projectDirectory, "Views"));
            var projectIsRazorPages   = Directory.Exists(Path.Combine(projectDirectory, "Pages"));
            var baseHrefPropertyValue = (projectIsMvc || projectIsRazorPages) ? BaseHrefMvc : BaseHrefApi;

            var filePath = Path.Combine(projectDirectory, jsonFileName);

            if (File.Exists(filePath))
            {
                var rootObj = JObject.Parse(File.ReadAllText(filePath));

                var parentObj = FindInsertPlace2(rootObj, projectName);
                if (parentObj != null)
                {
                    if (parentObj[BaseHrefPropertyName] == null)
                    {
                        var baseHrefProperty = new JProperty(BaseHrefPropertyName, baseHrefPropertyValue);
                        parentObj.Add(baseHrefProperty);

                        NgWizardHelper.RewriteFile(filePath, rootObj.ToString());
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #3
0
        /// <summary>
        /// Try to merge and return the operation result.
        /// </summary>
        /// <param name="projectDirectory"></param>
        /// <returns>null/false/true = not-applicable/failure/success</returns>
        private static bool?MergePackageJsonFiles(string projectDirectory)
        {
            const string ScriptsName         = "scripts";
            const string DependenciesName    = "dependencies";
            const string DevDependenciesName = "devDependencies";
            const string OldNameSuffix       = "_old";

            var filePath    = Path.Combine(projectDirectory, NgWizardHelper.PackageJsonFileName);
            var oldFilePath = Path.Combine(projectDirectory, NgWizardHelper.PackageJsonOldFileName);

            if (File.Exists(oldFilePath))
            {
                if (!File.Exists(filePath))
                {
                    return(false);
                }
            }
            else
            {
                return((bool?)null);
            }

            try
            {
                var oldObj = JObject.Parse(File.ReadAllText(oldFilePath));
                var newObj = JObject.Parse(File.ReadAllText(filePath));

                // We will give a higher priority in the three main sections to Ng, but keep the metadata properties from the old file.
                var resultObj = new JObject(oldObj);
                // The argument's content (i.e. newObj) wins over the "this" file (i.e. result).
                resultObj.Merge(newObj);

                // Clone the old content and delete the three main sections. Leave the metadata properties intact.
                var oObj = new JObject(oldObj);

                var propScr = oObj.Property(ScriptsName);
                if (propScr != null)
                {
                    propScr.Remove();
                }
                var propDep = oObj.Property(DependenciesName);
                if (propDep != null)
                {
                    propDep.Remove();
                }
                var propDev = oObj.Property(DevDependenciesName);
                if (propDev != null)
                {
                    propDev.Remove();
                }

                // Restore the old metadata properties.
                resultObj.Merge(oObj);

                // Add the three main sections from the old file for reference.
                var oScr = oldObj[ScriptsName];
                var oDep = oldObj[DependenciesName];
                var oDev = oldObj[DevDependenciesName];

                resultObj.Property(ScriptsName).AddAfterSelf(new JProperty(ScriptsName + OldNameSuffix, oScr ?? new JObject()));
                resultObj.Property(DependenciesName).AddAfterSelf(new JProperty(DependenciesName + OldNameSuffix, oDep ?? new JObject()));
                resultObj.Property(DevDependenciesName).AddAfterSelf(new JProperty(DevDependenciesName + OldNameSuffix, oDev ?? new JObject()));

                NgWizardHelper.RewriteFile(filePath, resultObj.ToString());
                return(true);
            }
            catch (Exception)
            {
            }
            return(false);
        }