Esempio n. 1
0
        private static EvaluationResult Relocate(Context context, AbsolutePath receiver, EvaluationResult[] args, EvaluationStackFrame captures)
        {
            var pathTable = context.FrontEndContext.PathTable;

            Args.AsPathOrDirectory(args, 0, out AbsolutePath sourceContainer, out DirectoryArtifact sourceDirContainer);
            sourceContainer = sourceDirContainer.IsValid ? sourceDirContainer.Path : sourceContainer;

            if (!receiver.IsWithin(pathTable, sourceContainer))
            {
                string message = string.Format(CultureInfo.CurrentCulture, $"Relocated path '{receiver.ToString(pathTable)}' is not inside the relocation source '{sourceContainer.ToString(pathTable)}'");
                throw new InvalidPathOperationException(message, new ErrorContext(pos: 1));
            }

            Args.AsPathOrDirectory(args, 1, out AbsolutePath targetContainer, out DirectoryArtifact targetDirContainer);
            targetContainer = targetDirContainer.IsValid ? targetDirContainer.Path : targetContainer;

            object newExtension = Args.AsIs(args, 2);

            AbsolutePath result;

            if (newExtension == UndefinedValue.Instance)
            {
                result = receiver.Relocate(pathTable, sourceContainer, targetContainer);
            }
            else
            {
                var      stringTable = context.FrontEndContext.StringTable;
                PathAtom newExt      = Converter.ExpectPathAtomFromStringOrPathAtom(stringTable, args[2], new ConversionContext(pos: 3));

                result = receiver.Relocate(pathTable, sourceContainer, targetContainer, newExt);
            }

            return(result.IsValid ? EvaluationResult.Create(result) : EvaluationResult.Undefined);
        }
        private static EvaluationResult WriteFileHelper(Context context, ModuleLiteral env, EvaluationStackFrame args, WriteFileMode mode)
        {
            var path        = Args.AsPath(args, 0, false);
            var tags        = Args.AsStringArrayOptional(args, 2);
            var description = Args.AsStringOptional(args, 3);

            PipData pipData;

            switch (mode)
            {
            case WriteFileMode.WriteFile:
                var fileContent = Args.AsIs(args, 1);
                // WriteFile has a separator argument with default newline
                var separator = Args.AsStringOptional(args, 3) ?? Environment.NewLine;
                description = Args.AsStringOptional(args, 4);

                pipData = CreatePipDataForWriteFile(context, fileContent, separator);
                break;

            case WriteFileMode.WriteData:
                var data = Args.AsIs(args, 1);
                pipData = DataProcessor.ProcessData(context, context.FrontEndContext.PipDataBuilderPool, EvaluationResult.Create(data), new ConversionContext(pos: 1));
                break;

            case WriteFileMode.WriteAllLines:
                var lines   = Args.AsArrayLiteral(args, 1);
                var entry   = context.TopStack;
                var newData = ObjectLiteral.Create(
                    new List <Binding>
                {
                    new Binding(context.Names.DataSeparator, Environment.NewLine, entry.InvocationLocation),
                    new Binding(context.Names.DataContents, lines, entry.InvocationLocation),
                },
                    lines.Location,
                    entry.Path);

                pipData = DataProcessor.ProcessData(context, context.FrontEndContext.PipDataBuilderPool, EvaluationResult.Create(newData), new ConversionContext(pos: 1));
                break;

            case WriteFileMode.WriteAllText:
                var text = Args.AsString(args, 1);
                pipData = DataProcessor.ProcessData(context, context.FrontEndContext.PipDataBuilderPool, EvaluationResult.Create(text), new ConversionContext(pos: 1));
                break;

            default:
                throw Contract.AssertFailure("Unknown WriteFileMode.");
            }

            FileArtifact result;

            if (!context.GetPipConstructionHelper().TryWriteFile(path, pipData, WriteFileEncoding.Utf8, tags, description, out result))
            {
                // Error has been logged
                return(EvaluationResult.Error);
            }

            return(new EvaluationResult(result));
        }
Esempio n. 3
0
        private EvaluationResult DumpData(Context context, ModuleLiteral env, EvaluationStackFrame args)
        {
            var pathTable = context.FrontEndContext.PathTable;
            var data      = Args.AsIs(args, 0);

            string dataAsString = null;

            switch (data)
            {
            case string s:
                dataAsString = s;
                break;

            case IImplicitPath pathData:
                dataAsString = pathData.Path.ToString(context.PathTable);
                break;

            case PathAtom pathAtom:
                dataAsString = pathAtom.ToString(context.StringTable);
                break;

            case RelativePath relativePath:
                dataAsString = relativePath.ToString(context.StringTable);
                break;

            case int n:
                dataAsString = n.ToString(CultureInfo.InvariantCulture);
                break;

            default:     // This is effectively only for object literals
                // Slow path
                dataAsString = DataProcessor.ProcessData(context.StringTable, m_dataSeparator, m_dataContents, context.FrontEndContext.PipDataBuilderPool, EvaluationResult.Create(data), new ConversionContext(pos: 1)).ToString(context.PathTable);
                break;
            }

            return(EvaluationResult.Create(dataAsString));
        }
Esempio n. 4
0
        private EvaluationResult WriteFileHelper(Context context, ModuleLiteral env, EvaluationStackFrame args, WriteFileMode mode)
        {
            AbsolutePath path;

            string[] tags;
            string   description;
            PipData  pipData;

            if (args.Length > 0 && args[0].Value is ObjectLiteral)
            {
                var obj = Args.AsObjectLiteral(args, 0);
                path        = Converter.ExtractPath(obj, m_writeOutputPath, allowUndefined: false);
                tags        = Converter.ExtractStringArray(obj, m_writeTags, allowUndefined: true);
                description = Converter.ExtractString(obj, m_writeDescription, allowUndefined: true);
                switch (mode)
                {
                case WriteFileMode.WriteData:
                    var data = obj[m_writeContents];
                    pipData = ProcessData(context, data, new ConversionContext(pos: 1));
                    break;

                case WriteFileMode.WriteAllLines:
                    var lines   = Converter.ExtractArrayLiteral(obj, m_writeLines);
                    var entry   = context.TopStack;
                    var newData = ObjectLiteral.Create(
                        new List <Binding>
                    {
                        new Binding(m_dataSeparator, Environment.NewLine, entry.InvocationLocation),
                        new Binding(m_dataContents, lines, entry.InvocationLocation),
                    },
                        lines.Location,
                        entry.Path);

                    pipData = ProcessData(context, EvaluationResult.Create(newData), new ConversionContext(pos: 1));
                    break;

                case WriteFileMode.WriteAllText:
                    var text = Converter.ExtractString(obj, m_writeText);
                    pipData = ProcessData(context, EvaluationResult.Create(text), new ConversionContext(pos: 1));
                    break;

                default:
                    throw Contract.AssertFailure("Unknown WriteFileMode.");
                }
            }
            else
            {
                path        = Args.AsPath(args, 0, false);
                tags        = Args.AsStringArrayOptional(args, 2);
                description = Args.AsStringOptional(args, 3);

                switch (mode)
                {
                case WriteFileMode.WriteFile:
                    var fileContent = Args.AsIs(args, 1);
                    // WriteFile has a separator argument with default newline
                    var separator = Args.AsStringOptional(args, 3) ?? Environment.NewLine;
                    description = Args.AsStringOptional(args, 4);

                    pipData = CreatePipDataForWriteFile(context, fileContent, separator);
                    break;

                case WriteFileMode.WriteData:
                    var data = Args.AsIs(args, 1);
                    pipData = ProcessData(context, EvaluationResult.Create(data), new ConversionContext(pos: 1));
                    break;

                case WriteFileMode.WriteAllLines:
                    var lines   = Args.AsArrayLiteral(args, 1);
                    var entry   = context.TopStack;
                    var newData = ObjectLiteral.Create(
                        new List <Binding>
                    {
                        new Binding(m_dataSeparator, Environment.NewLine, entry.InvocationLocation),
                        new Binding(m_dataContents, lines, entry.InvocationLocation),
                    },
                        lines.Location,
                        entry.Path);

                    pipData = ProcessData(context, EvaluationResult.Create(newData), new ConversionContext(pos: 1));
                    break;

                case WriteFileMode.WriteAllText:
                    var text = Args.AsString(args, 1);
                    pipData = ProcessData(context, EvaluationResult.Create(text), new ConversionContext(pos: 1));
                    break;

                default:
                    throw Contract.AssertFailure("Unknown WriteFileMode.");
                }
            }

            FileArtifact result;

            if (!context.GetPipConstructionHelper().TryWriteFile(path, pipData, WriteFileEncoding.Utf8, tags, description, out result))
            {
                // Error has been logged
                return(EvaluationResult.Error);
            }

            return(new EvaluationResult(result));
        }