/// <inheritdoc />
        public Task <Possible <ISourceFile> > TryParseAsync(AbsolutePath pathToParse, AbsolutePath moduleOrConfigPathPromptingParse, ParsingOptions parsingOption = null)
        {
            Contract.Assume(m_descriptorsBySpecPath != null, "Init must have been called");

            var pathToParseAsString = pathToParse.ToString(m_context.PathTable);

            if (!m_descriptorsBySpecPath.TryGetValue(pathToParse, out var descriptor))
            {
                return(Task.FromResult <Possible <ISourceFile> >(new SpecNotOwnedByResolverFailure(pathToParseAsString)));
            }

            if (!Downloads.TryGetValue(descriptor.Name, out var downloadData))
            {
                Contract.Assert(false, "Inconsistent internal state of NugetWorkspaceResolver");
                return(Task.FromResult <Possible <ISourceFile> >(new SpecNotOwnedByResolverFailure(pathToParseAsString)));
            }

            var sourceFile = SourceFile.Create(pathToParseAsString);

            var downloadDeclarationType = new TypeReferenceNode("File");

            downloadDeclarationType.TypeName.Pos = 1;
            downloadDeclarationType.TypeName.End = 2;
            var downloadDeclaration = new VariableDeclaration(downloadData.DownloadedValueName, Identifier.CreateUndefined(), downloadDeclarationType);

            downloadDeclaration.Flags |= NodeFlags.Export | NodeFlags.Public | NodeFlags.ScriptPublic;
            downloadDeclaration.Pos    = 1;
            downloadDeclaration.End    = 2;

            var extractedDeclarationType = new TypeReferenceNode("StaticDirectory");

            extractedDeclarationType.TypeName.Pos = 3;
            extractedDeclarationType.TypeName.Pos = 4;
            var extractedDeclaration = new VariableDeclaration(downloadData.ExtractedValueName, Identifier.CreateUndefined(), extractedDeclarationType);

            extractedDeclaration.Flags |= NodeFlags.Export | NodeFlags.Public | NodeFlags.ScriptPublic;
            extractedDeclaration.Pos    = 3;
            extractedDeclaration.End    = 4;

            sourceFile.Statements.Add(
                new VariableStatement()
            {
                DeclarationList = new VariableDeclarationList(
                    NodeFlags.Const,
                    downloadDeclaration,
                    extractedDeclaration)
            }
                );

            // Needed for the binder to recurse.
            sourceFile.ExternalModuleIndicator = sourceFile;
            sourceFile.SetLineMap(new [] { 0, 2 });

            return(Task.FromResult <Possible <ISourceFile> >(sourceFile));
        }
Exemplo n.º 2
0
        private IStatement CreatePackageVariableDeclaration(NugetAnalyzedPackage package)
        {
            IExpression       pkgExpression;
            TypeReferenceNode pkgType;

            if (package.IsManagedPackage)
            {
                // If the package is managed, it is a 'ManagedNugetPackage' and we create a switch based on the current qualifie
                // that defines contents, compile, runtime and dependency items
                pkgType = new TypeReferenceNode("Managed", "ManagedNugetPackage");

                // Computes the switch cases, based on the target framework
                List <ICaseClause> cases = CreateSwitchCasesForTargetFrameworks(package, pkgType);

                pkgExpression = new CallExpression(
                    new ParenthesizedExpression(
                        new ArrowFunction(
                            CollectionUtilities.EmptyArray <IParameterDeclaration>(),
                            new SwitchStatement(
                                PropertyAccess("qualifier", "targetFramework"),
                                new DefaultClause(
                                    new ExpressionStatement(
                                        new CallExpression(
                                            PropertyAccess("Contract", "fail"),
                                            new LiteralExpression("Unsupported target framework")))),
                                cases))));
            }
            else
            {
                // If the package is not managed, it is a 'NugetPackage' with just contents and dependencies
                pkgType       = new TypeReferenceNode("NugetPackage");
                pkgExpression = ObjectLiteral(
                    (name: "contents", PropertyAccess("Contents", "all")),
                    (name: "dependencies", Array(package.Dependencies.Select(CreateImportFromForDependency).ToArray())),
                    (name: "version", new LiteralExpression(package.Version)));
            }

            return
                (new VariableDeclarationBuilder()
                 .Name("pkg")
                 .Visibility(Visibility.Public)
                 .Initializer(pkgExpression)
                 .Type(pkgType)
                 .Build());
        }
Exemplo n.º 3
0
        private String typeReferenceToString(TypeReferenceNode typeRef)
        {
            var sb = new StringBuilder();

            switch (typeRef.TypeReferenceKind)
            {
            case Int:
            case Long:
            case Short:
            case Float:
            case Double:
            case Boolean:
            case Byte:
            case Void:
            case String:
                sb.append(typeRef.TypeReferenceKind.toString().toLowerCase());
                break;

            case SimpleName:
                var simpleName = (SimpleNameTypeReferenceNode)typeRef;
                sb.append(new String(text, simpleName.NameOffset, simpleName.NameLength));
                if (simpleName.TypeArguments.size() > 0)
                {
                    formatTypeArguments(simpleName.TypeArguments, sb);
                }
                break;

            case Qualified:
                var qtype = (QualifiedTypeReferenceNode)typeRef;
                sb.append(typeReferenceToString(qtype.EnclosingType));
                sb.append(".");
                sb.append(typeReferenceToString(qtype.SimpleName));
                break;

            case Array:
                var array = (ArrayTypeReferenceNode)typeRef;
                sb.append(typeReferenceToString(array.ElementType));
                sb.append("[]");
                break;

            default:
                throw new RuntimeException("Unhandled type kind: " + typeRef.TypeReferenceKind);
            }
            return(sb.toString());
        }