Exemplo n.º 1
0
        public override bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!base.Initialize(rawName, container, context))
            {
                return(false);
            }

            if (!J4JSoftware.Roslyn.TargetFramework.CreateTargetFramework(rawName, out var tgtFramework, Logger))
            {
                return(false);
            }

            if (!GetProperty <ExpandoObject>(container, "projectReferences", context, out var refContainer, optional: true))
            {
                return(false);
            }

            LoadFromContainer <ProjectReference, ExpandoObject>(refContainer, _refCreator, context, out var refList, containerCanBeNull: true);

            TargetFramework   = tgtFramework.Framework;
            TargetVersion     = tgtFramework.Version;
            ProjectReferences = refList;

            return(true);
        }
Exemplo n.º 2
0
        protected virtual bool ValidateInitializationArguments <TContainer>(
            string rawName,
            TContainer container,
            ProjectAssetsContext context,
            [CallerMemberName] string callerName = "")
        {
            if (container == null)
            {
                Logger.Error($"Undefined {nameof( container )}, {nameof( rawName )} is '{rawName}' (called from {GetCallerPath( callerName )})");

                return(false);
            }

            if (String.IsNullOrEmpty(rawName))
            {
                Logger.Error($"Undefined or empty {nameof( rawName )}  (called from {GetCallerPath( callerName )})");

                return(false);
            }

            if (context == null)
            {
                Logger.Error($"Undefined or empty {nameof( context )}  (called from {GetCallerPath( callerName )})");

                return(false);
            }

            return(true);
        }
Exemplo n.º 3
0
        protected string GetPropertyPath <TContainer>(TContainer toFind, ProjectAssetsContext context)
        {
            StringBuilder sb            = new StringBuilder();
            var           propertyStack = new Stack <string>();

            if (TraverseContainerTree <TContainer>(toFind, context.RootContainer, propertyStack))
            {
                while (propertyStack.Count > 0)
                {
                    if (sb.Length > 0)
                    {
                        sb.Insert(0, "->");
                    }
                    sb.Insert(0, propertyStack.Pop());
                }
            }

            if (sb.Length > 0)
            {
                sb.Insert(0, " [");
                sb.Append("]");
            }

            return(sb.ToString());
        }
Exemplo n.º 4
0
        public virtual bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 5
0
        public bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            if (!VersionedText.CreateVersionedText(rawName, out var verText, Logger))
            {
                return(false);
            }

            Assembly     = verText.TextComponent;
            Version      = verText.Version;
            Dependencies = new List <DependencyInfo>();

            var asDict = (IDictionary <string, object>)container;

            // dependencies are optional
            if (!asDict.ContainsKey("dependencies"))
            {
                return(true);
            }

            var depDict = asDict["dependencies"] as ExpandoObject;

            if (depDict == null)
            {
                Logger.Error(
                    $"{nameof( container )} does not have a 'dependencies' property which is an {nameof( ExpandoObject )}");

                return(false);
            }

            var retVal = true;

            foreach (var kvp in depDict)
            {
                if (kvp.Value is string versionText)
                {
                    if (VersionedText.TryParseSemanticVersion(versionText, out var version, Logger))
                    {
                        var newItem = _diCreator();

                        newItem.Assembly = kvp.Key;
                        newItem.Version  = version;

                        Dependencies.Add(newItem);
                    }
                    else
                    {
                        retVal = false;
                    }
                }
            }
Exemplo n.º 6
0
        public bool Initialize(ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(container, context))
            {
                return(false);
            }

            var okay = GetProperty <string>(container, "version", context, out var versionText);

            okay &= GetProperty <ExpandoObject>(container, "restore", context, out var restoreContainer);
            okay &= GetProperty <ExpandoObject>(container, "frameworks", context, out var frameContainer);
            okay &= GetProperty <ExpandoObject>(container, "warningProperties", context, out var warnContainer,
                                                optional: true);

            if (!okay)
            {
                return(false);
            }

            if (!VersionedText.TryParseSemanticVersion(versionText, out var version, Logger))
            {
                return(false);
            }

            var restore = _restoreCreator();

            if (!restore.Initialize(restoreContainer, context))
            {
                return(false);
            }

            if (!LoadFromContainer <FrameworkReferences, ExpandoObject>(frameContainer, _fwCreator, context,
                                                                        out var fwList))
            {
                return(false);
            }

            LoadFromContainer <WarningProperty, List <string> >(warnContainer, _wpCreator, context, out var warnList,
                                                                containerCanBeNull: true);

            Version           = version;
            Restore           = restore;
            Frameworks        = fwList;
            WarningProperties = warnList;

            return(true);
        }
Exemplo n.º 7
0
        public bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            if (!GetProperty <string>(container, "privateAssets", context, out var privateAssets))
            {
                return(false);
            }

            LibraryName   = rawName;
            PrivateAssets = privateAssets;

            return(true);
        }
Exemplo n.º 8
0
        public bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            if (!GetProperty <string>(container, "projectPath", context, out var path))
            {
                return(false);
            }

            ProjectName = rawName;
            ProjectPath = path;

            return(true);
        }
Exemplo n.º 9
0
        public bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            if (!TargetFramework.CreateTargetFramework(rawName, out var tgtFramework, Logger))
            {
                return(false);
            }

            Target  = tgtFramework.Framework;
            Version = tgtFramework.Version;

            Packages = new List <ReferenceInfo>();

            var retVal = true;

            foreach (var kvp in container)
            {
                var newItem = _refCreator();

                if (kvp.Value is ExpandoObject childContainer)
                {
                    if (newItem.Initialize(kvp.Key, childContainer, context))
                    {
                        Packages.Add(newItem);
                    }
                    else
                    {
                        retVal = false;
                    }
                }
                else
                {
                    Logger.Error($"{kvp.Key} property is not a {nameof( ExpandoObject )}");

                    retVal = false;
                }
            }

            return(retVal);
        }
Exemplo n.º 10
0
        public virtual bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            if (!VersionedText.CreateVersionedText(rawName, out var verText, Logger))
            {
                return(false);
            }

            if (!GetProperty <string>(container, "type", context, out var libTypeText))
            {
                return(false);
            }

            if (!Enum.TryParse <ReferenceType>(libTypeText, true, out var libType))
            {
                Logger.Error(
                    $"Property 'type' ('{libTypeText}') isn't parseable to a {nameof( ReferenceType )}");

                return(false);
            }

            if (libType != Type)
            {
                Logger.Error($"Expected a {Type} library, encountered a {libType} instead");

                return(false);
            }

            if (!GetProperty <string>(container, "path", context, out var path))
            {
                return(false);
            }

            Assembly = verText.TextComponent;
            Version  = verText.Version;
            Path     = path;

            return(true);
        }
        public bool Initialize(string rawName, List <string> container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            if (String.IsNullOrEmpty(rawName))
            {
                Logger.Error($"Undefined or empty {nameof( rawName )}");

                return(false);
            }

            if (!J4JSoftware.Roslyn.TargetFramework.CreateTargetFramework(rawName, out var tgtFramework, Logger))
            {
                return(false);
            }

            TargetFramework = tgtFramework.Framework;
            TargetVersion   = tgtFramework.Version;
            Dependencies    = new List <RestrictedDependencyInfo>();

            var retVal = true;

            foreach (var entry in container)
            {
                var newItem = _depCreator();

                if (newItem.Initialize(entry))
                {
                    Dependencies.Add(newItem);
                }
                else
                {
                    retVal = false;
                }
            }

            return(retVal);
        }
Exemplo n.º 12
0
        public bool Initialize(string rawName, List <string> container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            WarningType warnType;

            if (!Enum.TryParse <WarningType>(rawName, true, out warnType))
            {
                Logger.Error($"Couldn't parse '{rawName}' as a {nameof(WarningType)}");

                return(false);
            }

            WarningType = warnType;
            Codes       = container;

            return(true);
        }
Exemplo n.º 13
0
        public virtual bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(rawName, container, context))
            {
                return(false);
            }

            if (!GetProperty <string>(container, "target", context, out var tgtTypeText))
            {
                return(false);
            }

            if (!Enum.TryParse <ReferenceType>(tgtTypeText, true, out var tgtType))
            {
                Logger.Error($"Couldn't parse '{tgtTypeText}' to a {nameof(ReferenceType)}");

                return(false);
            }

            Assembly   = rawName;
            TargetType = tgtType;

            // parse into individual version strings
            rawName = rawName.Replace("[", "")
                      .Replace(")", "")
                      .Replace(" ", "");

            var versionTexts = rawName.Split(',', StringSplitOptions.RemoveEmptyEntries);

            Versions = new List <SemanticVersion>();

            var retVal = true;

            foreach (var versionText in versionTexts)
            {
                if (VersionedText.TryParseSemanticVersion(versionText, out var version, Logger))
                {
                    Versions.Add(version);
                }
Exemplo n.º 14
0
        private bool FilterLibraries(ExpandoObject libDict, ReferenceType refType, ProjectAssetsContext context, out ExpandoObject result)
        {
            result = null;

            if (libDict == null)
            {
                Logger.Error($"Undefined {nameof(libDict)}");

                return(false);
            }

            result = new ExpandoObject();
            var allOKay = true;

            foreach (var kvp in libDict)
            {
                if (kvp.Value is ExpandoObject child)
                {
                    if (GetProperty <string>(child, "type", context, out var typeText) &&
                        Enum.TryParse <ReferenceType>(typeText, true, out var childRefType) &&
                        childRefType == refType)
                    {
                        if (!result.TryAdd(kvp.Key, kvp.Value))
                        {
                            Logger.Error($"Couldn't add {kvp.Key} to new {nameof(ExpandoObject)} in {nameof(FilterLibraries)}");

                            allOKay = false;
                        }
                    }
                }
            }

            if (!allOKay)
            {
                result = null;
            }

            return(allOKay);
        }
Exemplo n.º 15
0
        public override bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!base.Initialize(rawName, container, context))
            {
                return(false);
            }

            var okay = GetProperty <string>(container, "SHA512", context, out var sha512);

            okay &= GetProperty <string>(container, "path", context, out var path);
            okay &= GetProperty <List <string> >(container, "files", context, out var files);

            if (!okay)
            {
                return(false);
            }

            SHA512 = sha512;
            Files  = files;

            return(true);
        }
Exemplo n.º 16
0
        public override bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!base.Initialize(rawName, container, context))
            {
                return(false);
            }

            if (!GetProperty <string>(container, "msbuildProject", context, out var rawPath))
            {
                return(false);
            }

            var projPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(context.ProjectDirectory, rawPath));

            if (!IsFileSupported(projPath))
            {
                return(false);
            }

            ProjectFilePath = projPath;

            return(ParseProjectFile());
        }
Exemplo n.º 17
0
        protected bool LoadFromContainer <TItem, TContainer>(
            ExpandoObject container,
            Func <TItem> itemCreator,
            ProjectAssetsContext context,
            out List <TItem> result,
            bool containerCanBeNull = false,
            [CallerMemberName] string callerName = "")
            where TItem : IInitializeFromNamed <TContainer>
        {
            if (container == null)
            {
                result = null;

                if (containerCanBeNull)
                {
                    return(true);
                }

                Logger.Error($"Undefined {nameof( container )} (called from {GetCallerPath( callerName )})");

                return(false);
            }

            result = new List <TItem>();
            var isOkay = true;

            foreach (var kvp in container)
            {
                if (kvp.Value is TContainer childContainer)
                {
                    var newItem = itemCreator();

                    if (newItem.Initialize(kvp.Key, childContainer, context))
                    {
                        result.Add(newItem);
                    }
                    else
                    {
                        isOkay = false;
                    }
                }
                else
                {
                    // empty json arrays are always List<object>...which likely won't be the type of
                    // list defined by TContainer. so check for that case
                    if (kvp.Value is List <object> objArray && (objArray.Count <= 0))
                    {
                        continue;
                    }

                    Logger.Error(
                        $"{kvp.Key} property is not a {nameof(ExpandoObject)} (called from {GetCallerPath( callerName )}){GetPropertyPath( container, context )}");

                    isOkay = false;
                }
            }

            // wipe out collection if something went wrong
            if (!isOkay)
            {
                result = null;
            }

            return(isOkay);
        }
Exemplo n.º 18
0
        protected bool GetProperty <TProp>(
            ExpandoObject container,
            string propName,
            ProjectAssetsContext context,
            out TProp result,
            bool caseSensitive = false,
            bool optional      = false,
            [CallerMemberName] string callerName = "")
        {
            if (container == null)
            {
                Logger.Error(
                    $"Undefined {nameof(container)} (called from {GetCallerPath( callerName )})");
                result = default;

                return(false);
            }

            if (String.IsNullOrEmpty(propName))
            {
                Logger.Error(
                    $"Undefined/empty {nameof(propName)} (called from {GetCallerPath( callerName )}){GetPropertyPath( container, context )}");
                result = default;

                return(false);
            }

            var asDict = (IDictionary <string, object>)container;

            // ExpandoObject keys are always case sensitive...so if we want a case insensitive match we have to
            // go a bit convoluted...
            bool hasKey = false;

            if (caseSensitive)
            {
                hasKey = asDict.ContainsKey(propName);
            }
            else
            {
                // case insensitive matches
                switch (asDict.Keys.Count(k => k.Equals(propName, StringComparison.OrdinalIgnoreCase)))
                {
                case 0:
                    // no match; key not found so default value of hasKey is okay
                    break;

                case 1:
                    // replace the case-insensitive property name with the correctly-cased value
                    propName = asDict.Keys.First(k => k.Equals(propName, StringComparison.OrdinalIgnoreCase));
                    hasKey   = true;

                    break;

                default:
                    // multiple case-insensitive matches; case insensitive doesn't work
                    Logger.Error(
                        $"Multiple matching property names in {nameof(ExpandoObject)} for property name '{propName}' (called from {GetCallerPath( callerName )})");
                    break;
                }
            }

            if (!hasKey)
            {
                result = default;

                var mesg =
                    $"{nameof(container)} doesn't contain a {propName} property (called from {GetCallerPath( callerName )}){GetPropertyPath( container, context )}";

                // it's okay if optional properties don't exist
                if (optional)
                {
                    Logger.Information(mesg);

                    return(true);
                }

                Logger.Error(mesg);

                return(false);
            }

            if (asDict[propName] is TProp retVal)
            {
                result = retVal;

                return(true);
            }

            Logger.Error(
                $"The {propName} property is not a {typeof(TProp).Name} (called from {GetCallerPath( callerName )})");

            result = default;

            return(false);
        }
Exemplo n.º 19
0
        public bool Initialize(string projFilePath, string projectAssetsPath)
        {
            if (String.IsNullOrEmpty(projectAssetsPath))
            {
                Logger.Error($"Empty or undefined {projectAssetsPath}");

                return(false);
            }

            if (!File.Exists(projectAssetsPath))
            {
                Logger.Error($"File '{projectAssetsPath}' not accessible");

                return(false);
            }

            var opt = new JsonSerializerOptions();

            opt.Converters.Add(_paConverter);

            ExpandoObject container;

            try
            {
                container = JsonSerializer.Deserialize <ExpandoObject>(File.ReadAllText(projectAssetsPath), opt);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);

                return(false);
            }

            var context = new ProjectAssetsContext
            {
                ProjectAssetsJsonPath = projectAssetsPath,
                ProjectPath           = projFilePath,
                RootContainer         = container
            };

            var okay = GetProperty <ExpandoObject>(container, "targets", context, out var tgtDict);

            okay &= GetProperty <ExpandoObject>(container, "libraries", context, out var libDict);
            okay &= GetProperty <ExpandoObject>(container, "projectFileDependencyGroups", context,
                                                out var projFileDepDict);
            okay &= GetProperty <ExpandoObject>(container, "packageFolders", context, out var pkgDict);
            okay &= GetProperty <ExpandoObject>(container, "project", context, out var projDict);
            okay &= GetProperty <int>(container, "version", context, out var version);
            if (!okay)
            {
                return(false);
            }

            // separate the libraries into package libraries and project libraries so we can process
            // them separately
            if (!FilterLibraries(libDict, ReferenceType.Package, context, out var pkgLibDict))
            {
                return(false);
            }

            if (!FilterLibraries(libDict, ReferenceType.Project, context, out var projLibDict))
            {
                return(false);
            }

            var project = _projCreator();

            okay  = LoadFromContainer <TargetInfo, ExpandoObject>(tgtDict, _tgtCreator, context, out var tgtList);
            okay &= LoadFromContainer <PackageLibrary, ExpandoObject>(pkgLibDict, _pkgLibCreator, context,
                                                                      out var pkgLibList);
            okay &= LoadFromContainer <ProjectLibrary, ExpandoObject>(projLibDict, _projLibCreator, context,
                                                                      out var projLibList);
            okay &= LoadFromContainer <ProjectFileDependencyGroup, List <string> >(projFileDepDict, _pfdgCreator, context,
                                                                                   out var pfdgList);
            okay &= LoadNamesFromContainer(pkgDict, out var pkgList);
            okay &= project.Initialize(projDict, context);

            if (!okay)
            {
                return(false);
            }

            var projLib = _projLibCreator();

            if (!projLib.InitializeFromProjectFile(projFilePath))
            {
                return(false);
            }

            Version = version;
            Targets = tgtList;

            Libraries = new List <ILibraryInfo>();
            Libraries.AddRange(pkgLibList);
            Libraries.AddRange(projLibList);

            ProjectFileDependencyGroups = pfdgList;
            Project        = project;
            PackageFolders = pkgList;
            ProjectLibrary = projLib;

            return(true);
        }
Exemplo n.º 20
0
        public override bool Initialize(string rawName, ExpandoObject container, ProjectAssetsContext context)
        {
            if (!base.Initialize(rawName, container, context))
            {
                return(false);
            }

            if (!J4JSoftware.Roslyn.TargetFramework.CreateTargetFramework(rawName, out var tgtFramework, Logger))
            {
                return(false);
            }

            var okay = GetProperty <ExpandoObject>(container, "dependencies", context, out var depContainer);

            okay &= GetProperty <List <string> >(container, "imports", context, out var importTexts);
            okay &= GetProperty <ExpandoObject>(container, "frameworkReferences", context, out var fwContainer);
            okay &= GetProperty <bool>(container, "assetTargetFallback", context, out var fallback);
            okay &= GetProperty <bool>(container, "warn", context, out var warn);
            okay &= GetProperty <string>(container, "runtimeIdentifierGraphPath", context, out var rtGraph);

            if (!okay)
            {
                return(false);
            }

            okay = LoadFromContainer <DependencyList, ExpandoObject>(depContainer, _depListCreator, context,
                                                                     out var depList);
            okay &= LoadFromContainer <FrameworkLibraryReference, ExpandoObject>(fwContainer, _fwlCreator, context,
                                                                                 out var fwList);

            if (!okay)
            {
                return(false);
            }

            var importsValid = true;

            var imports = importTexts.Select(it =>
            {
                if (!J4JSoftware.Roslyn.TargetFramework.CreateTargetFramework(it, out var retVal, Logger))
                {
                    importsValid = false;

                    return(null);
                }

                return(retVal);
            })
                          .ToList();

            if (!importsValid)
            {
                return(false);
            }

            TargetFramework     = tgtFramework.Framework;
            TargetVersion       = tgtFramework.Version;
            Dependencies        = depList;
            Imports             = imports;
            AssetTargetFallback = fallback;
            Warn = warn;
            FrameworkLibraryReferences = fwList;
            RuntimeIdentifierGraphPath = rtGraph;

            return(true);
        }
Exemplo n.º 21
0
        public bool Initialize(ExpandoObject container, ProjectAssetsContext context)
        {
            if (!ValidateInitializationArguments(container, context))
            {
                return(false);
            }

            var okay = GetProperty <string>(container, "projectStyle", context, out var styleText);

            okay &= GetProperty <string>(container, "projectUniqueName", context, out var uniqueName);
            okay &= GetProperty <string>(container, "projectName", context, out var projName);
            okay &= GetProperty <string>(container, "projectPath", context, out var path);
            okay &= GetProperty <string>(container, "packagesPath", context, out var pkgPath, optional: true);
            okay &= GetProperty <string>(container, "outputPath", context, out var outPath);
            okay &= GetProperty <List <string> >(container, "fallbackFolders", context, out var fallbackList,
                                                 optional: true);
            okay &= GetProperty <List <string> >(container, "configFilePaths", context, out var configPaths,
                                                 optional: true);
            okay &= GetProperty <List <string> >(container, "originalTargetFrameworks", context, out var origFWText);
            okay &= GetProperty <ExpandoObject>(container, "sources", context, out var srcContainer, optional: true);

            if (!okay)
            {
                return(false);
            }

            if (!Enum.TryParse <ProjectStyle>(styleText, true, out var style))
            {
                Logger.Error($"Couldn't parse projectStyle text '{styleText}' as a {nameof(ProjectStyle)}");

                return(false);
            }

            var origFWValid = true;

            var tgtFrameworks = origFWText.Select(t =>
            {
                if (TargetFramework.CreateTargetFramework(t, out var retVal, Logger))
                {
                    return(retVal);
                }

                origFWValid = false;

                return(null);
            })
                                .ToList();

            if (!origFWValid)
            {
                return(false);
            }

            List <string> sources = null;

            if (srcContainer != null && !LoadNamesFromContainer(srcContainer, out sources))
            {
                return(false);
            }

            ProjectStyle             = style;
            ProjectUniqueName        = uniqueName;
            ProjectName              = projName;
            ProjectPath              = path;
            PackagesPath             = pkgPath;
            OriginalTargetFrameworks = tgtFrameworks;
            OutputPath             = outPath;
            FallbackFolders        = fallbackList;
            ConfigurationFilePaths = configPaths;
            Sources = sources;

            return(true);
        }