예제 #1
0
        /// <summary>
        /// Creates a new module descriptor based on their name and optionally a version.
        /// </summary>
        /// <remarks>
        /// The module id is computed based on the module name hash code, so the id may be duplicated if module names are duplicated.
        /// This is used for testing purposes, so the module id can be predicted only by its name.
        /// </remarks>
        public static ModuleDescriptor CreateForTesting(string moduleName, string version = null, string resolverName = null)
        {
            var id = ModuleId.UnsafeCreate(moduleName.GetHashCode());

            return(new ModuleDescriptor(
                       id: id,
                       name: moduleName,
                       displayName: moduleName,
                       version: version,
                       resolverKind: KnownResolverKind.DScriptResolverKind,
                       resolverName: resolverName ?? "DScriptTestModule"));
        }
예제 #2
0
        protected Result <SpecsFromModuleResult, ResponseError> getSpecsForModule(JToken token)
        {
            var appState = m_getAppState();

            if (appState == null)
            {
                return(Result <SpecsFromModuleResult, ResponseError> .Error(new ResponseError
                {
                    code = ErrorCodes.InternalError,
                    message = BuildXL.Ide.LanguageServer.Strings.WorkspaceParsingFailedCannotPerformAction,
                }));
            }

            var getSpecsForModuleParams = token.ToObject <SpecsForModuleParams>();

            var workspace = appState.IncrementalWorkspaceProvider.WaitForRecomputationToFinish();

            var moduleDescriptor =

                workspace.TryGetModuleByModuleDescriptor(
                    new BuildXLModuleDescriptor(
                        id: ModuleId.UnsafeCreate(getSpecsForModuleParams.ModuleDescriptor.Id),
                        name: getSpecsForModuleParams.ModuleDescriptor.Name,
                        displayName: getSpecsForModuleParams.ModuleDescriptor.Name,
                        version: getSpecsForModuleParams.ModuleDescriptor.Version,
                        resolverKind: getSpecsForModuleParams.ModuleDescriptor.ResolverKind,
                        resolverName: getSpecsForModuleParams.ModuleDescriptor.ResolverName),
                    out var specificModule);

            if (specificModule != null)
            {
                var specDescriptors = new List <SpecDescriptor>(specificModule.Specs.Count);
                foreach (var spec in specificModule.Specs)
                {
                    specDescriptors.Add(new SpecDescriptor {
                        FileName = spec.Value.FileName, Id = spec.Value.Id
                    });
                }
                return(Result <SpecsFromModuleResult, ResponseError> .Success(new SpecsFromModuleResult { Specs = specDescriptors.ToArray() }));
            }

            return(Result <SpecsFromModuleResult, ResponseError> .Error(new ResponseError
            {
                code = ErrorCodes.InvalidParams,
                message = string.Format(BuildXL.Ide.LanguageServer.Strings.ModuleNotFoundInWorkspace, getSpecsForModuleParams.ModuleDescriptor.Name)
            }));
        }
예제 #3
0
        private static object CreateInstance(BuildXLContext context, Type type, bool booleanDefault)
        {
            string path = A("x", "path");

            type = GetNonNullableType(type);

            if (type == typeof(bool))
            {
                return(booleanDefault);
            }

            if (type == typeof(double))
            {
                return((double)0.23423);
            }

            if (type == typeof(byte))
            {
                return((byte)123);
            }

            if (type == typeof(sbyte))
            {
                return((sbyte)123);
            }

            if (type == typeof(short))
            {
                return((short)123);
            }

            if (type == typeof(ushort))
            {
                return((ushort)123);
            }

            if (type == typeof(int))
            {
                return(123);
            }

            if (type == typeof(uint))
            {
                return((uint)123);
            }

            if (type == typeof(long))
            {
                return((long)123);
            }

            if (type == typeof(ulong))
            {
                return((ulong)123);
            }

            if (type == typeof(string))
            {
                return("nonDefaultString");
            }

            if (type == typeof(ModuleId))
            {
                return(ModuleId.UnsafeCreate(123));
            }

            if (type == typeof(LocationData))
            {
                return(new LocationData(AbsolutePath.Create(context.PathTable, path), 12, 23));
            }

            if (type == typeof(AbsolutePath))
            {
                return(AbsolutePath.Create(context.PathTable, path));
            }

            if (type == typeof(RelativePath))
            {
                string relativePath = R("rel1", "dir1", "path");
                return(RelativePath.Create(context.StringTable, relativePath));
            }

            if (type == typeof(FileArtifact))
            {
                return(FileArtifact.CreateSourceFile(AbsolutePath.Create(context.PathTable, path)));
            }

            if (type == typeof(PathAtom))
            {
                return(PathAtom.Create(context.StringTable, "atom"));
            }

            if (type == typeof(global::BuildXL.Utilities.LineInfo))
            {
                return(new global::BuildXL.Utilities.LineInfo(1, 1));
            }

            if (type.GetTypeInfo().IsEnum)
            {
                bool first = true;
                foreach (var value in Enum.GetValues(type))
                {
                    if (!first)
                    {
                        return(value);
                    }

                    first = false;
                }

                XAssert.Fail($"Enum {type.FullName} doesn't have more than one value, so can't pick the second one.");
            }

            if (type.GetTypeInfo().IsGenericType)
            {
                var generic = type.GetGenericTypeDefinition();

                if (generic == typeof(IReadOnlyList <>))
                {
                    // Treat IReadOnlyList as if it was List
                    type    = typeof(List <>).MakeGenericType(type.GenericTypeArguments[0]);
                    generic = type.GetGenericTypeDefinition();
                }

                if (generic == typeof(List <>))
                {
                    var newList = (IList)Activator.CreateInstance(type);
                    newList.Add(CreateInstance(context, type.GenericTypeArguments[0], booleanDefault));
                    return(newList);
                }

                if (generic == typeof(IReadOnlyDictionary <,>))
                {
                    // Treat IReadOnlyList as if it was List
                    type    = typeof(Dictionary <,>).MakeGenericType(type.GenericTypeArguments[0], type.GenericTypeArguments[1]);
                    generic = type.GetGenericTypeDefinition();
                }

                if (generic == typeof(Dictionary <,>))
                {
                    var newDictionary = (IDictionary)Activator.CreateInstance(type);
                    newDictionary.Add(
                        CreateInstance(context, type.GenericTypeArguments[0], booleanDefault),
                        CreateInstance(context, type.GenericTypeArguments[1], booleanDefault));
                    return(newDictionary);
                }

                if (generic == typeof(ValueTuple <,>))
                {
                    var newTuple = Activator.CreateInstance(type);

                    // In ValueTuple classes, the first 7 values are accessible via Item1-Item7 fields.
                    // The tuple field names (named tuples) aren't part of runtime representation.
                    type.GetField("Item1").SetValue(newTuple, CreateInstance(context, type.GenericTypeArguments[0], booleanDefault));
                    type.GetField("Item2").SetValue(newTuple, CreateInstance(context, type.GenericTypeArguments[1], booleanDefault));

                    return(newTuple);
                }
            }

            if (type.GetTypeInfo().IsInterface)
            {
                // Treat interfaces as if it was the mutable class
                type = ConfigurationConverter.FindImplementationType(
                    type,
                    ObjectLiteral.Create(new List <Binding>(), default(LineInfo), AbsolutePath.Invalid),

                    // Return a SourceResolver to instantiate
                    () => "SourceResolver");
            }

            if (type.GetTypeInfo().IsClass)
            {
                var instance = Activator.CreateInstance(type);
                PopulateObject(context, type, instance, booleanDefault);
                return(instance);
            }

            XAssert.Fail($"Don't know how to create objects for this type: {type.FullName}.");
            return(null);
        }