Exemplo n.º 1
0
        public static bool TryGetGlobalSettings(string path, out GlobalSettings solution)
        {
            solution = null;

            string solutionPath = null;

            if (Path.GetFileName(path) == GlobalFileName)
            {
                solutionPath = path;
                path = Path.GetDirectoryName(path);
            }
            else if (!HasGlobalFile(path))
            {
                return false;
            }
            else
            {
                solutionPath = Path.Combine(path, GlobalFileName);
            }

            solution = new GlobalSettings();

            string json = File.ReadAllText(solutionPath);
            var settings = JObject.Parse(json);
            var sources = settings["sources"];

            solution.SourcePaths = sources == null ? new string[] { } : sources.ToObject<string[]>();
            solution.PackagesPath = settings.Value<string>("packages");

            return true;
        }
Exemplo n.º 2
0
        public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings)
        {
            globalSettings = null;
            string globalJsonPath = null;

            if (Path.GetFileName(path) == GlobalFileName)
            {
                globalJsonPath = path;
                path = Path.GetDirectoryName(path);
            }
            else if (!HasGlobalFile(path))
            {
                return false;
            }
            else
            {
                globalJsonPath = Path.Combine(path, GlobalFileName);
            }

            globalSettings = new GlobalSettings();

            try
            {
                using (var fs = File.OpenRead(globalJsonPath))
                {
                    var reader = new StreamReader(fs);
                    var jobject = JsonDeserializer.Deserialize(reader) as JsonObject;

                    if (jobject == null)
                    {
                        throw new InvalidOperationException("The JSON file can't be deserialized to a JSON object.");
                    }

                    var projectSearchPaths = jobject.ValueAsStringArray("projects") ??
                                             jobject.ValueAsStringArray("sources") ??
                                             new string[] { };

                    globalSettings.ProjectSearchPaths = new List<string>(projectSearchPaths);
                    globalSettings.PackagesPath = jobject.ValueAsString("packages");
                    globalSettings.FilePath = globalJsonPath;
                }
            }
            catch (Exception ex)
            {
                throw FileFormatException.Create(ex, globalJsonPath);
            }

            return true;
        }
Exemplo n.º 3
0
        public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings)
        {
            globalSettings = null;
            string globalJsonPath = null;

            if (Path.GetFileName(path) == GlobalFileName)
            {
                globalJsonPath = path;
                path = Path.GetDirectoryName(path);
            }
            else if (!HasGlobalFile(path))
            {
                return false;
            }
            else
            {
                globalJsonPath = Path.Combine(path, GlobalFileName);
            }

            globalSettings = new GlobalSettings();

            string json = File.ReadAllText(globalJsonPath);
            JObject settings = null;

            try
            {
                settings = JObject.Parse(json);
            }
            catch (JsonReaderException ex)
            {
                throw FileFormatException.Create(ex, globalJsonPath);
            }

            // TODO: Remove sources
            var projectSearchPaths = settings["projects"] ?? settings["sources"];

            globalSettings.ProjectSearchPaths = projectSearchPaths == null ?
                new string[] { } :
                projectSearchPaths.ValueAsArray<string>();
            globalSettings.PackagesPath = settings.Value<string>("packages");
            globalSettings.FilePath = globalJsonPath;

            return true;
        }
Exemplo n.º 4
0
        public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings)
        {
            globalSettings = null;

            string globalJsonPath = null;

            if (Path.GetFileName(path) == GlobalFileName)
            {
                globalJsonPath = path;
                path = Path.GetDirectoryName(path);
            }
            else if (!HasGlobalFile(path))
            {
                return false;
            }
            else
            {
                globalJsonPath = Path.Combine(path, GlobalFileName);
            }

            globalSettings = new GlobalSettings();

            string json = File.ReadAllText(globalJsonPath);
            JObject settings = null;

            try
            {
                settings = JObject.Parse(json);
            }
            catch (JsonReaderException ex)
            {
                throw FileFormatException.Create(ex, globalJsonPath);
            }

            // TODO: Remove sources
            var projectSearchPaths = settings["projects"] ?? settings["sources"];
            var dependencies = settings["dependencies"] as JObject;

            globalSettings.ProjectSearchPaths = projectSearchPaths == null ? new string[] { } : projectSearchPaths.ToObject<string[]>();
            globalSettings.PackagesPath = settings.Value<string>("packages");
            globalSettings.PackageHashes = new Dictionary<Library, string>();
            globalSettings.FilePath = globalJsonPath;

            if (dependencies != null)
            {
                foreach (var property in dependencies.Properties())
                {
                    var dependencyValue = dependencies[property.Name] as JObject;
                    if (dependencyValue == null)
                    {
                        throw FileFormatException.Create(string.Format(
                            "The value of '{0}' in {1} must be an object", property.Name, GlobalFileName), property, globalJsonPath);
                    }

                    var versionToken = dependencyValue["version"];
                    var versionValue = versionToken?.ToString();

                    SemanticVersion version;
                    if (!SemanticVersion.TryParse(versionValue, out version))
                    {
                        throw FileFormatException.Create(string.Format(
                            "The dependency '{0}' in {1} doesn't have valid version information",
                            property.Name, GlobalFileName), versionToken, globalJsonPath);
                    }

                    var library = new Library()
                    {
                        Name = property.Name,
                        Version = version
                    };

                    var shaToken = dependencyValue["sha"];
                    var shaValue = shaToken?.ToString();

                    if (string.IsNullOrEmpty(shaValue))
                    {
                        throw FileFormatException.Create(string.Format(
                            "The dependency '{0}' in {1} doesn't have a valid SHA value",
                            property.Name, GlobalFileName), shaToken, globalJsonPath);
                    }

                    globalSettings.PackageHashes[library] = shaValue;
                }
            }

            return true;
        }
Exemplo n.º 5
0
        public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings)
        {
            globalSettings = null;

            string globalJsonPath = null;

            if (Path.GetFileName(path) == GlobalFileName)
            {
                globalJsonPath = path;
                path           = Path.GetDirectoryName(path);
            }
            else if (!HasGlobalFile(path))
            {
                return(false);
            }
            else
            {
                globalJsonPath = Path.Combine(path, GlobalFileName);
            }

            globalSettings = new GlobalSettings();

            string  json     = File.ReadAllText(globalJsonPath);
            JObject settings = null;

            try
            {
                settings = JObject.Parse(json);
            }
            catch (JsonReaderException ex)
            {
                throw FileFormatException.Create(ex, globalJsonPath);
            }

            // TODO: Remove sources
            var projectSearchPaths = settings["projects"] ?? settings["sources"];
            var dependencies       = settings["dependencies"] as JObject;

            globalSettings.ProjectSearchPaths = projectSearchPaths == null ? new string[] { } : projectSearchPaths.ToObject <string[]>();
            globalSettings.PackagesPath       = settings.Value <string>("packages");
            globalSettings.PackageHashes      = new Dictionary <Library, string>();
            globalSettings.FilePath           = globalJsonPath;

            if (dependencies != null)
            {
                foreach (var property in dependencies.Properties())
                {
                    var dependencyValue = dependencies[property.Name] as JObject;
                    if (dependencyValue == null)
                    {
                        throw FileFormatException.Create(string.Format(
                                                             "The value of '{0}' in {1} must be an object", property.Name, GlobalFileName), property, globalJsonPath);
                    }

                    var versionToken = dependencyValue["version"];
                    var versionValue = versionToken?.ToString();

                    SemanticVersion version;
                    if (!SemanticVersion.TryParse(versionValue, out version))
                    {
                        throw FileFormatException.Create(string.Format(
                                                             "The dependency '{0}' in {1} doesn't have valid version information",
                                                             property.Name, GlobalFileName), versionToken, globalJsonPath);
                    }

                    var library = new Library()
                    {
                        Name    = property.Name,
                        Version = version
                    };

                    var shaToken = dependencyValue["sha"];
                    var shaValue = shaToken?.ToString();

                    if (string.IsNullOrEmpty(shaValue))
                    {
                        throw FileFormatException.Create(string.Format(
                                                             "The dependency '{0}' in {1} doesn't have a valid SHA value",
                                                             property.Name, GlobalFileName), shaToken, globalJsonPath);
                    }

                    globalSettings.PackageHashes[library] = shaValue;
                }
            }

            return(true);
        }