Esempio n. 1
0
        public static int Init(InitOptions options)
        {
            var workspaceRoot = Directory.GetCurrentDirectory();
            var workspaceName = options.WorkspaceName;

            if (string.IsNullOrEmpty(workspaceName))
            {
                workspaceName = Path.GetFileName(workspaceRoot);
                ;
            }
            else
            {
                workspaceRoot = Path.Combine(workspaceRoot, workspaceName);
                Directory.CreateDirectory(workspaceRoot);
                Directory.SetCurrentDirectory(workspaceRoot);
            }

            var runfiles       = Runfiles.Create();
            var templates      = Templates.CreateDefault(runfiles);
            var workspaceMaker = new WorkspaceMaker(workspaceRoot, workspaceName, templates);

            workspaceMaker.Init();

            Console.WriteLine("Workspace created, next steps:");
            Console.WriteLine("bazel run //:gazelle");
            Console.WriteLine("bazel build //...");
            Console.WriteLine("bazel test //...");
            return(0);
        }
Esempio n. 2
0
        private static string?FindGazelle(Runfiles runfiles)
        {
            // are we released?
            var    artifactsFolder = runfiles.Rlocation($"rules_msbuild/.azpipelines/artifacts");
            string?gazellePath     = null;

            // if we're in nuget, runfiles will be directory-based, and this will work just fine on windows
            if (Directory.Exists(artifactsFolder))
            {
                var subPath = Util.GetGazellePath();
                gazellePath = Path.Combine(artifactsFolder, subPath);
            }
            else
            {
                string suffix = "";
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    suffix = ".exe";
                }
                gazellePath =
                    runfiles.Rlocation($"rules_msbuild/gazelle/dotnet/gazelle-dotnet_/gazelle-dotnet{suffix}");
            }

            if (gazellePath == null || !File.Exists(gazellePath))
            {
                Console.Error.WriteLine(
                    $"Failed to locate gazelle-dotnet binary at path {gazellePath}, please file an issue at github.com/samhowes/rules_msbuild");
                return(null);
            }

            return(gazellePath);
        }
Esempio n. 3
0
        public void TestCreatesManifestBasedRunfiles()
        {
            using var mf = new MockFile("a/b c/d");
            var r =
                Runfiles.Create(
                    new Dictionary <string, string>
            {
                { "RUNFILES_MANIFEST_ONLY", "1" },
                { "RUNFILES_MANIFEST_FILE", mf.Path },
                { "RUNFILES_DIR", "ignored when RUNFILES_MANIFEST_ONLY=1" },
                {
                    "TEST_SRCDIR", "should always be ignored"
                }
            });

            r.Rlocation("a/b").Should().Be("c/d");
            r.Rlocation("foo").Should().BeNull();

            if (IsWindows())
            {
                r.Rlocation("c:/foo").Should().Be("c:/foo");
                r.Rlocation("c:\\foo").Should().Be("c:\\foo");
            }
            else
            {
                r.Rlocation("/foo").Should().Be("/foo");
            }
        }
Esempio n. 4
0
        static int Main(string[] args)
        {
            // for testing building an executable in an external workspace
            if (args[0] == "ping")
            {
                Console.WriteLine("pong");
                return(0);
            }
            Debug(string.Join(" ", args));
            Debug(Environment.CurrentDirectory);
            if (args.Length != 1)
            {
                return(Fail($"Expected config file as only argument. Got {String.Join(" ", args)}"));
            }

            var runfiles = Runfiles.Create();

            var config = JsonSerializer.Deserialize <TestConfig>(File.ReadAllText(args[0]), new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });

            config !.ReleaseTar = runfiles.Rlocation(config.ReleaseTar);
            config !.Bazel      = runfiles.Rlocation(config.Bazel);

            Info($"Using release tar {config!.ReleaseTar}");
            Info($"Using input workspace {config!.WorkspaceRoot}");

            using var testRunner = new TestRunner(runfiles, config);
            var exitCode = testRunner.Run();

            Debug($"testRunner exit code is {exitCode}");
            return(exitCode);
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            var runfiles = Runfiles.Create();
            var foo      = runfiles.Rlocation("rules_msbuild/tests/examples/NuGet/Tool/foo.txt");

            Console.WriteLine("runfile contents: " + File.ReadAllText(foo));
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            var runfiles = Runfiles.Create();
            var path     = runfiles.Rlocation("hello/RunfilesTest/foo.txt");

            Console.WriteLine(File.ReadAllText(path));
        }
Esempio n. 7
0
        public void TestDirectoryBasedEnvVars()
        {
            var dir =
                Files.CreateTempDirectory();

            var envVars =
                Runfiles.Create(
                    new Dictionary <string, string>
            {
                {
                    "RUNFILES_MANIFEST_FILE",
                    "ignored when RUNFILES_MANIFEST_ONLY is not set to 1"
                },
                {
                    "RUNFILES_DIR",
                    dir
                },
                {
                    "JAVA_RUNFILES",
                    "ignored when RUNFILES_DIR has a value"
                },
                {
                    "TEST_SRCDIR",
                    "should always be ignored"
                }
            })
                .GetEnvVars();

            envVars.Keys.Should().Equal("RUNFILES_DIR");
            envVars["RUNFILES_DIR"].Should().Be(dir);
        }
Esempio n. 8
0
        public void TestManifestBasedCtorArgumentValidation()
        {
            AssertThrows <ArgumentException>(() => Runfiles.CreateManifestBasedForTesting(null));
            AssertThrows <ArgumentException>(() => Runfiles.CreateManifestBasedForTesting(""));

            using var mf = new MockFile("a b");
            Runfiles.CreateManifestBasedForTesting(mf.Path);
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            var r        = Runfiles.Create();
            var contents =
                File.ReadAllText(r.Rlocation("rules_msbuild/tests/dotnet/tools/runfiles/integration/foo.txt"));

            Console.Write(contents);
        }
Esempio n. 10
0
        private void AssertRlocationArg(Runfiles runfiles, string path, string error)
        {
            var e =
                AssertThrows <ArgumentException>(() => runfiles.Rlocation(path));

            if (error != null)
            {
                e.Message.Should().Contain(error);
            }
        }
Esempio n. 11
0
        public void TestFailsToCreateManifestBasedBecauseManifestDoesNotExist()
        {
            var e = AssertThrows <FileNotFoundException>(
                () => Runfiles.Create(new Dictionary <string, string>
            {
                { "RUNFILES_MANIFEST_ONLY", "1" },
                { "RUNFILES_MANIFEST_FILE", "non-existing path" }
            }));

            e.Message.Should().Contain("non-existing path");
        }
Esempio n. 12
0
        public static string UpdateWorkspaceTemplate(Runfiles runfiles, string tarPath, string url)
        {
            var workspaceTemplate =
                File.ReadAllText(runfiles.Rlocation("rules_msbuild/dotnet/tools/Bzl/WORKSPACE.tpl"));
            var sha256 = File.ReadAllText(tarPath + ".sha256");

            var indexMatch = Regex.Match(workspaceTemplate, @"http_archive\(.*\s+name = ""rules_msbuild"",",
                                         RegexOptions.IgnoreCase | RegexOptions.Singleline);

            var index   = (indexMatch.Index + indexMatch.Length);
            var section = workspaceTemplate[index..];
Esempio n. 13
0
        public void TestDirectoryBasedCtorArgumentValidation()
        {
            AssertThrows <ArgumentException>(
                () => Runfiles.CreateDirectoryBasedForTesting(null));

            AssertThrows <ArgumentException>(() => Runfiles.CreateDirectoryBasedForTesting(""));

            AssertThrows <ArgumentException>(
                () => Runfiles.CreateDirectoryBasedForTesting("non-existent directory is bad"));

            Runfiles.CreateDirectoryBasedForTesting(BazelEnvironment.GetTmpDir());
        }
Esempio n. 14
0
        public void TestManifestBasedRlocation()
        {
            using var mf = new MockFile(
                      "Foo/runfile1 C:/Actual Path\\runfile1",
                      "Foo/Bar/runfile2 D:\\the path\\run file 2.txt");

            var r = Runfiles.CreateManifestBasedForTesting(mf.Path);

            r.Rlocation("Foo/runfile1").Should().Be("C:/Actual Path\\runfile1");
            r.Rlocation("Foo/Bar/runfile2").Should().Be("D:\\the path\\run file 2.txt");
            r.Rlocation("unknown").Should().BeNull();
        }
Esempio n. 15
0
        public void TestDirectoryBasedRlocation()
        {
            // The DirectoryBased implementation simply joins the runfiles directory and the runfile's path
            // on a "/". DirectoryBased does not perform any normalization, nor does it check that the path
            // exists.
            var dir = Path.Combine(BazelEnvironment.GetTmpDir() !, "mock/runfiles");

            Directory.CreateDirectory(dir).Exists.Should().Be(true);

            var r = Runfiles.CreateDirectoryBasedForTesting(dir);

            // Escaping for "\": once for string and once for regex.
            r.Rlocation("arg").Should().MatchRegex(@".*[/\\]mock[/\\]runfiles[/\\]arg");
        }
Esempio n. 16
0
        public static int Gazelle(GazelleOptions options)
        {
            var runfiles = Runfiles.Create();
            var gazelle  = FindGazelle(runfiles);

            if (gazelle == null)
            {
                return(1);
            }

            var process = Process.Start(gazelle);

            process !.WaitForExit();
            return(process.ExitCode);
        }
Esempio n. 17
0
        private static int Test(string tarPath)
        {
            var workspaceRoot = Directory.GetCurrentDirectory();
            var workspaceName = Path.GetFileName(workspaceRoot);
            var runfiles      = Runfiles.Create();
            var templates     = Templates.CreateDefault(runfiles);

            var workspaceContents =
                Util.UpdateWorkspaceTemplate(runfiles, tarPath, $"file:{tarPath}");

            templates.Workspace = new Template("WORKSPACE", workspaceContents);
            var workspaceMaker = new WorkspaceMaker(workspaceRoot, workspaceName, templates);

            workspaceMaker.Init();
            return(0);
        }
Esempio n. 18
0
        public void TestCreatesDirectoryBasedRunfiles()
        {
            var dir =
                Files.CreateTempDirectory();

            var r =
                Runfiles.Create(
                    new Dictionary <string, string>
            {
                { "RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1" },
                { "RUNFILES_DIR", dir },
                { "TEST_SRCDIR", "should always be ignored" }
            });

            r.Rlocation("a/b").Should().EndWith("/a/b");
            r.Rlocation("foo").Should().EndWith("/foo");
        }
Esempio n. 19
0
        public void TestManifestBasedEnvVars()
        {
            var dir = Files.CreateTempDirectory();

            var mf = Path.Combine(dir, "MANIFEST");

            File.WriteAllLines(mf, Array.Empty <string>(), Encoding.UTF8);

            var envvars =
                Runfiles.Create(
                    new Dictionary <string, string>
            {
                { "RUNFILES_MANIFEST_ONLY", "1" },
                { "RUNFILES_MANIFEST_FILE", mf },
                { "RUNFILES_DIR", "ignored when RUNFILES_MANIFEST_ONLY=1" },
                { "JAVA_RUNFILES", "ignored when RUNFILES_DIR has a value" },
                { "TEST_SRCDIR", "should always be ignored" }
            })
                .GetEnvVars();

            envvars.Keys.Should().Equal("RUNFILES_MANIFEST_ONLY", "RUNFILES_MANIFEST_FILE", "RUNFILES_DIR");

            envvars["RUNFILES_MANIFEST_ONLY"].Should().Be("1");
            envvars["RUNFILES_MANIFEST_FILE"].Should().Be(mf);
            envvars["RUNFILES_DIR"].Should().Be(dir);
            var rfDir = Path.Combine(dir, "foo.runfiles");

            Directory.CreateDirectory(rfDir);
            mf = Path.Combine(dir, "foo.runfiles_manifest");
            File.WriteAllLines(mf, Array.Empty <string>(), Encoding.UTF8);
            envvars =
                Runfiles.Create(
                    new Dictionary <string, string>
            {
                { "RUNFILES_MANIFEST_ONLY", "1" },
                { "RUNFILES_MANIFEST_FILE", mf },
                { "RUNFILES_DIR", "ignored when RUNFILES_MANIFEST_ONLY=1" },
                { "TEST_SRCDIR", "should always be ignored" }
            })
                .GetEnvVars();
            envvars["RUNFILES_MANIFEST_ONLY"].Should().Be("1");
            envvars["RUNFILES_MANIFEST_FILE"].Should().Be(mf);
            envvars["RUNFILES_DIR"].Should().Be(rfDir);
        }
Esempio n. 20
0
        public void TestRlocationArgumentValidation()
        {
            var dir =
                Files.CreateTempDirectory();

            var r = Runfiles.Create(new Dictionary <string, string> {
                { "RUNFILES_DIR", dir }
            }).Runfiles;

            AssertRlocationArg(r, null, null);
            AssertRlocationArg(r, "", null);
            AssertRlocationArg(r, "../foo", "is not normalized");
            AssertRlocationArg(r, "foo/..", "is not normalized");
            AssertRlocationArg(r, "foo/../bar", "is not normalized");
            AssertRlocationArg(r, "./foo", "is not normalized");
            AssertRlocationArg(r, "foo/.", "is not normalized");
            AssertRlocationArg(r, "foo/./bar", "is not normalized");
            AssertRlocationArg(r, "//foobar", "is not normalized");
            AssertRlocationArg(r, "foo//", "is not normalized");
            AssertRlocationArg(r, "foo//bar", "is not normalized");
            AssertRlocationArg(r, "\\foo", "path is absolute without a drive letter");
        }
Esempio n. 21
0
        static void Main(string[] args)
        {
            var runfiles = Runfiles.Create();

            var root    = BazelEnvironment.GetWorkspaceRoot();
            var tocList = new List <(string, string)>();

            for (var i = 0; i < args.Length; i += 2)
            {
                var label = new Label(args[i]);
                var file  = args[i + 1];
                Info($"Processing: {label.RawValue} => {file}");
                var rlocation = runfiles.Rlocation($"rules_msbuild/{file}");
                var contents  = File.ReadAllText(rlocation);

                var destRel = string.Join('/', "docs", Path.GetFileName(label.Package) + ".md");
                var dest    = Path.GetFullPath(Path.Combine(root, destRel));

                contents = LabelLinkRegex.Replace(contents, (match) =>
                {
                    var labelLink = new Label(match.Groups["label"].Value);
                    string package;
                    if (labelLink.IsRelative)
                    {
                        package = labelLink.Package;
                    }
                    else
                    {
                        package = labelLink.Package;
                    }

                    var labelRel   = string.Join('/', package, labelLink.Name);
                    var targetFile = Path.GetFullPath(Path.Combine(root, labelRel));
                    var rel        = Path.GetRelativePath(Path.GetDirectoryName(dest) !, targetFile).Replace('\\', '/');
                    Info($"{labelLink.RawValue} => {rel}");
                    return($"[{match.Groups["link_text"]}]({rel})");
                });
Esempio n. 22
0
 public TestRunner(Runfiles runfiles, TestConfig config)
 {
     _runfiles = runfiles;
     _config   = config;
 }
Esempio n. 23
0
        public static Templates CreateDefault(Runfiles runfiles)
        {
            var templates = new Templates();

            var toLoad = new (string, string, string)[]