Esempio n. 1
0
        public void ReadFile_RestoresPaths()
        {
            SaveFile_ReplacesPaths();

            ResetCache();

            var project = _cache.LoadProject("foo");

            var path = project !.Properties.FirstOrDefault(p => p.Name == "FilePath");

            path.Should().NotBeNull();
            path !.EvaluatedValue.Should().Be(Restored);
        }
Esempio n. 2
0
        private ProjectInstance CreateProjectInstanceImpl(string projectPath, Dictionary <string, string> globalProperties,
                                                          ProjectCollection projectCollection)
        {
            var project = _cache.LoadProject(projectPath);

            if (project == null)
            {
                if (projectPath != _entryProjectPath)
                {
                    var manifestPath = _pathMapper.ToManifestPath(projectPath);
                    throw new ProjectCacheMissException(projectPath);
                }
                foreach (var(name, value) in projectCollection.GlobalProperties)
                {
                    globalProperties[name] = value;
                }
                project = new ProjectInstance(
                    projectPath,
                    globalProperties,
                    "Current",
                    projectCollection);
            }
            else
            {
                project.LateInitialize(projectCollection.ProjectRootElementCache, null);
            }
            Environment.CurrentDirectory = project.Directory;

            if (_cache.Project == null && project.FullPath == _entryProjectPath)
            {
                _cache.Project = project;
                EntryProject   = project;
            }

            if (_targetGraph != null)
            {
                var path    = _pathMapper.ToBazel(projectPath);
                var cluster = _targetGraph.GetOrAddCluster(path, null);
                void AddTargets(TargetGraph.Node thisTarget, string targetString, TargetBuiltReason reason)
                {
                    foreach (var beforeName in targetString.Split(";")
                             .Where(s => !string.IsNullOrEmpty(s) && !string.IsNullOrWhiteSpace(s)))
                    {
                        if (beforeName.StartsWith("$"))
                        {
                            var property = GetProperty(beforeName, project);
                            if (property == null)
                            {
                                continue;
                            }
                            AddTargets(thisTarget, property.EvaluatedValue, reason);
                            return;
                        }
                        var other = cluster !.GetOrAdd(beforeName);
                        TargetGraph.Node parent = null !;
                        TargetGraph.Node child  = null !;
                        switch (reason)
                        {
                        case TargetBuiltReason.BeforeTargets:
                            parent = other;
                            child  = thisTarget;
                            break;

                        case TargetBuiltReason.DependsOn:
                        case TargetBuiltReason.AfterTargets:
                        default:
                            parent = thisTarget;
                            child  = other;
                            break;
                        }

                        parent.AddDependency(child, reason);
                    }
                }

                foreach (var(targetName, target) in project.Targets)
                {
                    var thisTarget = cluster.GetOrAdd(targetName);
                    AddTargets(thisTarget, target.DependsOnTargets, TargetBuiltReason.DependsOn);
                    AddTargets(thisTarget, target.AfterTargets, TargetBuiltReason.AfterTargets);
                    AddTargets(thisTarget, target.BeforeTargets, TargetBuiltReason.BeforeTargets);

                    foreach (var buildTask in target.Tasks)
                    {
                        List <string>?projects = null;
                        switch (buildTask.Name.ToLower())
                        {
                        case "msbuild":
                            if (buildTask.Parameters.TryGetValue("Projects", out var projectsString))
                            {
                                projects = projectsString.Split(";").ToList();
                            }
                            else
                            {
                                throw new Exception(":(");
                            }

                            break;

                        case "calltarget":
                            projects = new List <string>()
                            {
                                "$(MSBuildProjectFullPath)"
                            };
                            break;

                        default:
                            continue;
                        }

                        foreach (var projectValue in projects)
                        {
                            if (projectValue.Contains("RestoreGraph"))
                            {
                                continue;
                            }
                            List <string>?defaultTargets = null;
                            Cluster       targetCluster  = cluster;
                            var           first          = projectValue[0];
                            if (projectValue[1] == ':') // windows
                            {
                                first = '/';
                            }
                            switch (first)
                            {
                            case '/':
                            case '%':
                            case '@':
                                // these items likely won't have anything in them yet, since we haven't dont a build yet.
                                // we'll make them a "meta" cluster
                                var bazelPath = _pathMapper.ToBazel(projectValue);
                                targetCluster = _targetGraph.GetOrAddCluster(bazelPath, null);
                                break;

                            case '$':
                                switch (projectValue)
                                {
                                case "$(MSBuildProjectFullPath)":
                                    defaultTargets = project.DefaultTargets;
                                    break;

                                case "$(MSBuildThisFileFullPath)":
                                {
                                    var builtProject = buildTask.FullPath;
                                    var sdkIndex     = builtProject.ToLower().IndexOf("sdk");
                                    if (sdkIndex >= 0)
                                    {
                                        builtProject = builtProject[sdkIndex..];
                                    }

                                    targetCluster = _targetGraph.GetOrAddCluster(builtProject, null);
                                    break;
                                }