Ejemplo n.º 1
0
        public void GetExtensionTests()
        {
            var st = new StringTable(0);

            // get a single char extension
            PathAtom e1 = RelativePath.Create(st, "a.c").GetExtension(st);

            XAssert.AreEqual(@".c", e1.ToString(st));

            // get a multi char extension
            e1 = RelativePath.Create(st, "a.cpp").GetExtension(st);
            XAssert.AreEqual(@".cpp", e1.ToString(st));

            // get nothing
            e1 = RelativePath.Create(st, "a").GetExtension(st);
            XAssert.IsFalse(e1.IsValid);

            // get a single char extension
            e1 = RelativePath.Create(st, "a.c.d").GetExtension(st);
            XAssert.AreEqual(@".d", e1.ToString(st));

            // get a multi char extension
            e1 = RelativePath.Create(st, ".cpp").GetExtension(st);
            XAssert.AreEqual(@".cpp", e1.ToString(st));
        }
Ejemplo n.º 2
0
        public void GetExtension()
        {
            var pt = new PathTable();

            // get a single char extension
            PathAtom e1 = AbsolutePath.Create(pt, @"/a.c").GetExtension(pt);

            XAssert.AreEqual(@".c", e1.ToString(pt.StringTable));

            // get a multi char extension
            e1 = AbsolutePath.Create(pt, @"/a.cpp").GetExtension(pt);
            XAssert.AreEqual(@".cpp", e1.ToString(pt.StringTable));

            // get nothing
            e1 = AbsolutePath.Create(pt, @"/a").GetExtension(pt);
            XAssert.IsFalse(e1.IsValid);

            // get a single char extension
            e1 = AbsolutePath.Create(pt, @"/a.c.d").GetExtension(pt);
            XAssert.AreEqual(@".d", e1.ToString(pt.StringTable));

            // get a multi char extension
            e1 = AbsolutePath.Create(pt, @"/.cpp").GetExtension(pt);
            XAssert.AreEqual(@".cpp", e1.ToString(pt.StringTable));
        }
Ejemplo n.º 3
0
        public void ChangeExtension()
        {
            var st = new StringTable(0);

            // change a single char extension
            PathAtom pa1 = PathAtom.Create(st, @"a.c");
            PathAtom pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));

            XAssert.AreEqual(@"a.d", pa2.ToString(st));

            // change a multi char extension
            pa1 = PathAtom.Create(st, @"a.cpp");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@"a.d", pa2.ToString(st));

            // change nothing
            pa1 = PathAtom.Create(st, @"a");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@"a.d", pa2.ToString(st));

            // change a single char extension
            pa1 = PathAtom.Create(st, @"ab.c");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@"ab.d", pa2.ToString(st));

            // change a multi char extension
            pa1 = PathAtom.Create(st, @"ab.cpp");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@"ab.d", pa2.ToString(st));

            // change nothing
            pa1 = PathAtom.Create(st, @"ab");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@"ab.d", pa2.ToString(st));

            // change a single char extension
            pa1 = PathAtom.Create(st, @"ab.xyz.c");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@"ab.xyz.d", pa2.ToString(st));

            // change a multi char extension
            pa1 = PathAtom.Create(st, @"ab.xyz.cpp");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@"ab.xyz.d", pa2.ToString(st));

            pa1 = PathAtom.Create(st, @".cpp");
            pa2 = pa1.ChangeExtension(st, PathAtom.Create(st, ".d"));
            XAssert.AreEqual(@".d", pa2.ToString(st));

            pa1 = PathAtom.Create(st, @"a.cpp");
            pa2 = pa1.ChangeExtension(st, PathAtom.Invalid);
            XAssert.AreEqual(@"a", pa2.ToString(st));
        }
Ejemplo n.º 4
0
        public void GetName()
        {
            var st = new StringTable(0);

            RelativePath rp   = RelativePath.Create(st, @"AAA");
            PathAtom     atom = rp.GetName();

            XAssert.AreEqual(@"AAA", atom.ToString(st));

            rp   = RelativePath.Create(st, @"AAA\BBB");
            atom = rp.GetName();
            XAssert.AreEqual(@"BBB", atom.ToString(st));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Retrieve a string representation of a path atom.
        /// </summary>
        public string Expand(PathAtom atom)
        {
            Contract.Requires(atom.IsValid);
            Contract.Ensures(!string.IsNullOrEmpty(Contract.Result <string>()));

            return(atom.ToString(m_stringTable));
        }
Ejemplo n.º 6
0
        /// <inheritdoc />
        public override AbsolutePath ReadAbsolutePath()
        {
            // Read the index
            var index = ReadInt32Compact();

            // Check if string already encountered
            if (index > m_maxReadPathIndex)
            {
                AbsolutePath entryPath = AbsolutePath.Invalid;
                for (int i = m_maxReadPathIndex + 1; i <= index; i++)
                {
                    int          entryParentIndex = ReadInt32Compact();
                    PathAtom     entryPathName    = ReadPathAtom();
                    AbsolutePath parentPath       = m_readPaths[(uint)entryParentIndex];
                    entryPath = parentPath.IsValid ?
                                parentPath.Combine(m_pathTable, entryPathName) :
                                AbsolutePath.Create(m_pathTable, entryPathName.ToString(m_pathTable.StringTable) + Path.DirectorySeparatorChar);
                    m_readPaths[(uint)i] = entryPath;
                }

                m_maxReadPathIndex = index;
                return(entryPath);
            }

            return(m_readPaths[(uint)index]);
        }
Ejemplo n.º 7
0
        public void GetName()
        {
            var          pt   = new PathTable();
            AbsolutePath da   = AbsolutePath.Create(pt, @"/a");
            PathAtom     atom = da.GetName(pt);

            XAssert.AreEqual(@"a", atom.ToString(pt.StringTable));
        }
Ejemplo n.º 8
0
            private string ToString(PathAtom key)
            {
                if (!key.IsValid)
                {
                    return("<no extension>");
                }

                return(key.ToString(m_analyzer.StringTable));
            }
Ejemplo n.º 9
0
        private IExpression Generate(PathAtom pathAtom)
        {
            if (pathAtom.IsValid)
            {
                return(new TaggedTemplateExpression("a", pathAtom.ToString(m_stringTable)));
            }

            return(Identifier.CreateUndefined());
        }
Ejemplo n.º 10
0
        public void Concat()
        {
            var st = new StringTable(0);

            PathAtom a1 = PathAtom.Create(st, "AAA");
            PathAtom a2 = PathAtom.Create(st, "BBB");
            PathAtom a3 = a1.Concat(st, a2);

            XAssert.AreEqual("AAABBB", a3.ToString(st));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets a relative path unique for the current call.
        /// </summary>
        private RelativePath GetUniqueRelativePath(PathAtom name)
        {
            var count       = m_folderIdResolver.GetNextId(name);
            var stringTable = Context.StringTable;

            var pathAtom = count == 0
                ? name
                : PathAtom.Create(stringTable, string.Concat(name.ToString(stringTable), "_", count.ToString(CultureInfo.InvariantCulture)));

            return(PipRelativePath.Combine(pathAtom));
        }
Ejemplo n.º 12
0
        public void ToStringTest()
        {
            var st = new StringTable(0);

            Assert.Throws <NotImplementedException>(() =>
            {
                PathAtom a = PathAtom.Create(st, "AAA");
#pragma warning disable 618
                a.ToString();
#pragma warning restore 618
            });
        }
Ejemplo n.º 13
0
        public void IsInitialized()
        {
            PathAtom atom = default(PathAtom);

            XAssert.IsFalse(atom.IsValid);

            var st = new StringTable(0);

            atom = PathAtom.Create(st, "AAA");
            XAssert.AreEqual("AAA", atom.ToString(st));
            XAssert.IsTrue(atom.IsValid);
        }
Ejemplo n.º 14
0
 private static void AssertEqualPathAtoms(BuildXLContext context, string objPath, Remapper remapper, PathAtom expectedAtom, PathAtom actualAtom)
 {
     if (remapper?.PathAtomStringRemapper != null)
     {
         var expectedAtomString = remapper.PathAtomStringRemapper(expectedAtom.ToString(context.StringTable));
         XAssert.AreEqual(
             expectedAtomString,
             actualAtom.ToString(remapper.PathTable.StringTable),
             $"{nameof(PathAtom)} values don't match for objPath: {objPath}");
     }
     else
     {
         XAssert.AreEqual(expectedAtom, actualAtom, $"{nameof(PathAtom)} values don't match for objPath: {objPath}");
     }
 }
Ejemplo n.º 15
0
        /// <nodoc />
        public PathAtom Remap(PathAtom pathAtom)
        {
            if (m_oldPathTable == null || !pathAtom.IsValid)
            {
                return(pathAtom);
            }

            var pathAtomString = pathAtom.ToString(m_oldPathTable.StringTable);

            if (m_pathAtomStringRemapper != null)
            {
                pathAtomString = m_pathAtomStringRemapper(pathAtomString);
            }

            return(PathAtom.Create(m_newPathTable.StringTable, pathAtomString));
        }
Ejemplo n.º 16
0
        public void RemoveExtension()
        {
            var st = new StringTable(0);

            // remove a single char extension
            PathAtom pa1 = PathAtom.Create(st, @"a.c");
            PathAtom pa2 = pa1.RemoveExtension(st);

            XAssert.AreEqual(@"a", pa2.ToString(st));

            // remove a multi char extension
            pa1 = PathAtom.Create(st, @"a.cpp");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(@"a", pa2.ToString(st));

            // remove nothing
            pa1 = PathAtom.Create(st, @"a");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(pa1, pa2);

            // remove a single char extension
            pa1 = PathAtom.Create(st, @"ab.c");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(@"ab", pa2.ToString(st));

            // remove a multi char extension
            pa1 = PathAtom.Create(st, @"ab.cpp");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(@"ab", pa2.ToString(st));

            // remove nothing
            pa1 = PathAtom.Create(st, @"ab");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(pa1, pa2);

            pa1 = PathAtom.Create(st, @"ab.xyz.c");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(@"ab.xyz", pa2.ToString(st));

            // remove a multi char extension
            pa1 = PathAtom.Create(st, @"ab.xyz.cpp");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(@"ab.xyz", pa2.ToString(st));

            pa1 = PathAtom.Create(st, @".cpp");
            pa2 = pa1.RemoveExtension(st);
            XAssert.AreEqual(pa1, pa2);
        }
Ejemplo n.º 17
0
        private AbsolutePath ResolvePathWithCache(
            AbsolutePath accessPath,
            [CanBeNull] string accessPathAsString,
            ReportedFileOperation operation,
            FlagsAndAttributes flagsAndAttributes,
            [CanBeNull] out string resolvedPathAsString,
            out bool isDirectoryReparsePoint)
        {
            accessPathAsString ??= accessPath.ToString(m_context.PathTable);
            // If the final segment is a directory reparse point and the operation acts on it, don't resolve it
            if (ShouldNotResolveLastSegment(ExpandedAbsolutePath.CreateUnsafe(accessPath, accessPathAsString), operation, flagsAndAttributes, out isDirectoryReparsePoint))
            {
                AbsolutePath resolvedParentPath = ResolvePathWithCache(accessPath.GetParent(m_context.PathTable), accessPathAsString: null, operation, flagsAndAttributes, out string resolvedParentPathAsString, out _);
                PathAtom     name = accessPath.GetName(m_context.PathTable);
                resolvedPathAsString = resolvedParentPathAsString != null?Path.Combine(resolvedParentPathAsString, name.ToString(m_context.StringTable)) : null;

                return(resolvedParentPath.Combine(m_context.PathTable, name));
            }

            // Check the cache
            var cachedResult = m_resolvedPathCache.TryGet(accessPath);

            if (cachedResult.IsFound)
            {
                // Observe we don't cache expanded paths since that might cause the majority of all the paths of a build to live
                // in memory for the whole execution time. So in case of a cache hit, we just return the absolute path. The expanded
                // path needs to be reconstructed as needed.
                resolvedPathAsString = null;
                return(cachedResult.Item.Value);
            }

            // If not found and the path is not a reparse point, check the cache for its parent.
            // Many files may have the same parent, and since the path is not a reparse point we know that
            // resolve(parent\segment) == resolve(parent)\segment
            var parentPath = accessPath.GetParent(m_context.PathTable);

            if (parentPath.IsValid && !isDirectoryReparsePoint && m_resolvedPathCache.TryGet(parentPath) is var cachedParent && cachedParent.IsFound)
            {
                var lastFragment = accessPath.GetName(m_context.PathTable);
                resolvedPathAsString = null;
                AbsolutePath cachedParentPath = cachedParent.Item.Value;
                return(cachedParentPath.IsValid ?
                       cachedParentPath.Combine(m_context.PathTable, lastFragment) :
                       AbsolutePath.Create(m_context.PathTable, lastFragment.ToString(m_context.StringTable)));
            }

            // The cache didn't have it, so let's resolve it
            if (!TryResolveSymlinkedPath(accessPathAsString, out ExpandedAbsolutePath resolvedExpandedPath))
            {
                // If we cannot get the final path (e.g. file not found causes this), then we assume the path
                // is already canonicalized

                // Observe we cannot update the cache since the path was not resolved.
                // TODO: we could consider always trying to resolve the parent

                resolvedPathAsString = accessPathAsString;

                return(accessPath);
            }

            // Update the cache
            m_resolvedPathCache.TryAdd(accessPath, resolvedExpandedPath.Path);

            // If the path is not a reparse point, update the parent as well
            if (!isDirectoryReparsePoint && parentPath.IsValid)
            {
                // Observe we may be storing an invalid path if the resolved path does not have a parent.
                m_resolvedPathCache.TryAdd(parentPath, resolvedExpandedPath.Path.GetParent(m_context.PathTable));
            }

            // If the resolved path is the same as the access path, then we know its last fragment is not a reparse point.
            // So we might just update the symlink cache as well and hopefully speed up subsequent requests to generate read accesses
            // for intermediate symlink dirs
            if (accessPath == resolvedExpandedPath.Path)
            {
                while (parentPath.IsValid && m_symlinkCache.TryAdd(parentPath, false))
                {
                    parentPath = parentPath.GetParent(m_context.PathTable);
                }
            }

            resolvedPathAsString = resolvedExpandedPath.ExpandedPath;
            return(resolvedExpandedPath.Path);
        }
Ejemplo n.º 18
0
 private static bool IsModuleConfigFileName(PathAtom fileName, StringTable stringTable)
 {
     return(ExtensionUtilities.IsModuleConfigurationFile(fileName.ToString(stringTable)));
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Deals with cases like 'lib/portable-net45+win8+wpa81/System.Threading.Tasks.Dataflow.dll'. Splits the compound target framework
        /// directory using '-' and '+' as separators and tries to find the first fragment that matches a known target framework.
        /// </summary>
        /// <remarks>
        /// To avoid ambiguity the first framework that gets evaluated and contains a known moniker succeeds, but subsequent
        /// compound frameworks that would resolve to the same moniker fail. In that way, the first known framework is mapped to the same moniker and other
        /// candidates are ignored.
        /// </remarks>
        private bool TryGetKnownTargetFramework(PathAtom targetFrameworkFolder, out NugetTargetFramework targetFramework)
        {
            Contract.Assert(targetFrameworkFolder.IsValid);

            var targetFrameworkFragments = targetFrameworkFolder.ToString(m_context.StringTable).Split('+', '-');

            // If there are no + or -, then the moniker and the target framework folder are equivalent
            // This is the most common case
            if (targetFrameworkFragments.Length == 1)
            {
                if (NugetFrameworkMonikers.WellknownMonikers.Contains(targetFrameworkFolder))
                {
                    // If this is the first time we see this known moniker, we record it
                    // so no other (compound) target frameworks are used for the same folder
                    if (!m_monikerToTargetFramework.ContainsKey(targetFrameworkFolder))
                    {
                        m_monikerToTargetFramework.Add(targetFrameworkFolder, targetFrameworkFolder);
                    }

                    targetFramework = new NugetTargetFramework(targetFrameworkFolder);
                    return(true);
                }
            }

            foreach (var target in targetFrameworkFragments)
            {
                if (!PathAtom.TryCreate(m_context.StringTable, target, out Moniker moniker))
                {
                    targetFramework = default(NugetTargetFramework);
                    return(false);
                }

                // Check if we saw a compound framework before mapped to the same moniker
                if (m_monikerToTargetFramework.ContainsKey(moniker))
                {
                    // We saw it and it's the same target framework folder
                    if (m_monikerToTargetFramework[moniker] == targetFrameworkFolder)
                    {
                        targetFramework = new NugetTargetFramework(moniker, targetFrameworkFolder);
                        return(true);
                    }

                    // We saw it, but the folder is different, so we make it fail
                    targetFramework = default(NugetTargetFramework);
                    return(false);
                }

                // We didn't see this compound framework, so we check if it maps to a known moniker and if
                // that's the case we update the compound monikers seen so far and we return it
                if (NugetFrameworkMonikers.WellknownMonikers.Contains(moniker))
                {
                    m_monikerToTargetFramework.Add(moniker, targetFrameworkFolder);

                    targetFramework = new NugetTargetFramework(moniker, targetFrameworkFolder);
                    return(true);
                }
            }

            targetFramework = default(NugetTargetFramework);
            return(false);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Retrieve a string representation of a path atom.
        /// </summary>
        public string Expand(PathAtom atom)
        {
            Contract.Requires(atom.IsValid);

            return(atom.ToString(m_stringTable));
        }
Ejemplo n.º 21
0
 public XElement CreateRow(string key, PathAtom value)
 {
     return(value.IsValid ? CreateRow(key, value.ToString(m_stringTable)) : null);
 }
Ejemplo n.º 22
0
        public ObjectInfo GetObjectInfo(object context, object obj)
        {
            obj = obj is EvaluationResult evalResult
                ? evalResult.Value
                : obj;

            if (obj == null || IsInvalid(obj))
            {
                return(s_nullObj);
            }

            if (obj.GetType().IsArray)
            {
                return(ArrayObjInfo(((IEnumerable)obj).Cast <object>().ToArray()));
            }

            var customResult = m_customRenderer?.Invoke(this, context, obj);

            if (customResult != null)
            {
                return(customResult);
            }

            return(obj switch
            {
                ScopeLocals scope => new ObjectInfo(LocalsScopeName, null, Lazy.Create(() => GetLocalsForStackEntry(scope.EvalState, scope.FrameIndex))),
                ScopePipGraph scope => PipGraphInfo(scope.Graph).WithPreview(PipGraphScopeName),
                ScopeAllModules scope => ArrayObjInfo(scope.EvaluatedModules.ToArray()).WithPreview(EvaluatedModulesScopeName),
                IModuleAndContext mc => GetObjectInfo(mc.Tree.RootContext, mc.Module),
                ObjectInfo objInf => objInf,
                IPipGraph graph => PipGraphInfo(graph),
                Pip pip => GenericObjectInfo(pip, $"<{pip.PipType}>").Build(),
                PipProvenance prov => ProvenanceInfo(prov),
                EnvironmentVariable envVar => EnvironmentVariableInfo(envVar),
                PipFragment pipFrag => PipFragmentInfo(context, pipFrag),
                Thunk thunk => thunk.Value != null?GetObjectInfo(context, thunk.Value) : new ObjectInfo("<not evaluated>"),
                    FunctionLikeExpression lambda => LambdaInfo(lambda),
                    Closure cls => LambdaInfo(cls.Function),
                    SymbolAtom sym => new ObjectInfo(sym.ToString(StringTable)),
                    StringId id => new ObjectInfo(id.ToString(StringTable)),
                    PipId id => new ObjectInfo($"{id.Value}"),
                    UndefinedLiteral _ => new ObjectInfo("undefined", UndefinedLiteral.Instance),
                    UndefinedValue _ => new ObjectInfo("undefined", UndefinedValue.Instance),
                    AbsolutePath path => new ObjectInfo($"p`{path.ToString(PathTable)}`", path),
                    RelativePath path => new ObjectInfo($"r`{path.ToString(StringTable)}`", path),
                    PathAtom atom => new ObjectInfo($"a`{atom.ToString(StringTable)}`", atom),
                    FileArtifact file => new ObjectInfo($"f`{file.Path.ToString(PathTable)}`", file),
                    DirectoryArtifact dir => new ObjectInfo($"d`{dir.Path.ToString(PathTable)}`", dir),
                    int num => new ObjectInfo($"{num}"),
                    uint num => new ObjectInfo($"{num}"),
                    short num => new ObjectInfo($"{num}", (int)num),
                    long num => new ObjectInfo($"{num}"),
                    char ch => new ObjectInfo($"'{ch}'", ch.ToString()),
                    string str => new ObjectInfo($"\"{str}\"", str),
                    Enum e => new ObjectInfo($"{e.GetType().Name}.{e}", e),
                    NumberLiteral numLit => new ObjectInfo(numLit.UnboxedValue.ToString(), numLit),
                    Func <object> func => FuncObjInfo(func),
                    ArraySegment <object> arrSeg => ArrayObjInfo(arrSeg),
                    IEnumerable enu => new ObjectInfoBuilder().Preview("IEnumerable").Prop("Result", Lazy.Create <object>(() => enu.Cast <object>().ToArray())).Build(),
                    ArrayLiteral arrLit => ArrayObjInfo(arrLit.Values.Select(v => v.Value).ToArray()).WithOriginal(arrLit),
                    ModuleBinding binding => GetObjectInfo(context, binding.Body),
                    ErrorValue error => ErrorValueInfo(),
                    object o => GenericObjectInfo(o).Build(),
                    _ => s_nullObj
            });