예제 #1
0
        public IProjectFile <NuGetPackageReference> Load(string path)
        {
            var content           = File.ReadAllText(path);
            var scriptFileContent = new ScriptFileContent()
            {
                SourceCode = content
            };
            var matches = Regex.Matches(content, ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast <Match>()
                          .Union(Regex.Matches(content, LoadDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast <Match>()).ToArray();
            var packageReferences = new List <ScriptPackageReference>();

            foreach (var match in matches)
            {
                var packageName    = match.Groups[2].Value;
                var packageVersion = match.Groups[4].Value;

                if (FloatRange.TryParse(packageVersion, out var floatRange))
                {
                    var nugetVersion          = floatRange.MinVersion;
                    var nugetPackageReference = new ScriptPackageReference(packageName, packageVersion, floatRange, scriptFileContent);
                    packageReferences.Add(nugetPackageReference);
                }
                else
                {
                    console.WriteError($"Warning: The package '{packageName}' has an invalid version number '{packageVersion}'");
                }
            }

            return(new ScriptProjectFile(scriptFileContent, path)
            {
                PackageReferences = packageReferences.ToArray()
            });
        }
 public ScriptPackageReference(string name, string version, FloatRange floatRange, ScriptFileContent content) : base(name, version, floatRange)
 {
     referenceDirectivePattern = $@"^(\s*#r\s*""nuget:\s*)({name})(,\s*)(.*)(\s*""$)";
     loadDirectivePattern      = $@"^(\s*#load\s*""nuget:\s*)({name})(,\s*)(.*)(\s*""$)";
     this.content = content;
 }