Skip to content

Commit cbc6547

Browse files
authored
[xa-prep-tasks] Support autoprovision with brew 1.2 (#595)
We got a new macOS Jenkins machine, running brew 1.2. (Our other machines have brew 1.0 installed.) When attempting to build on this new machine, [things broke][0]: Executing: sudo brew install 'p7zip' Error: Running Homebrew as root is extremely dangerous. As Homebrew does not drop privileges on installation you are giving all build scripts full access to your system. As a result of the macOS sandbox not handling the root user correctly HOMEBREW_NO_SANDBOX has been set so the sandbox will not be used. If we have not merged a pull request to add privilege dropping by November 1st 2016 running Homebrew as root will be disabled. No Homebrew maintainers plan to work on this functionality. In short, using `sudo brew` doesn't work...on brew 1.2. It's currently *required* to use `sudo brew` on brew 1.0. After some crying, split this difference by running `brew --version`, and only use `sudo brew` when using brew 1.0. [0]: https://jenkins.mono-project.com/view/Xamarin.Android/job/xamarin-android-pr-builder/942/
1 parent c0f31f8 commit cbc6547

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/PrepareInstall.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public class PrepareInstall : Task
2424
[Output]
2525
public ITaskItem DownloadUrl { get; set; }
2626

27-
string Sudo {
28-
get {return (UseSudo && HostOS != "Windows") ? "sudo " : "";}
29-
}
27+
string Sudo;
3028

3129
public override bool Execute ()
3230
{
@@ -36,6 +34,7 @@ public override bool Execute ()
3634
Log.LogMessage (MessageImportance.Low, $" {nameof (Program)}: {Program}");
3735
Log.LogMessage (MessageImportance.Low, $" {nameof (UseSudo)}: {UseSudo}");
3836

37+
SetSudo ();
3938
SetDownloadUrl ();
4039
SetInstallCommand ();
4140

@@ -50,6 +49,27 @@ string GetHostProperty (string property)
5049
return Which.GetHostProperty (Program, property, HostOS, HostOSName);
5150
}
5251

52+
void SetSudo ()
53+
{
54+
if (!UseSudo || string.Equals ("Windows", HostOS, StringComparison.OrdinalIgnoreCase))
55+
return;
56+
57+
if (!string.Equals ("Darwin", HostOS, StringComparison.OrdinalIgnoreCase)) {
58+
Sudo = "sudo ";
59+
return;
60+
}
61+
string brewFilename;
62+
var brewPath = Which.GetProgramLocation ("brew", out brewFilename);
63+
if (string.IsNullOrEmpty (brewPath)) {
64+
return;
65+
}
66+
var brewVersion = Which.GetProgramVersion (HostOS, $"{brewPath} --version");
67+
if (brewVersion < new Version (1, 1)) {
68+
Sudo = "sudo ";
69+
return;
70+
}
71+
}
72+
5373
void SetDownloadUrl ()
5474
{
5575
var minUrl = GetHostProperty ("MinimumUrl");
@@ -78,7 +98,7 @@ void SetInstallCommand ()
7898
InstallCommand = Sudo + install;
7999
return;
80100
}
81-
if (HostOS == "Darwin") {
101+
if (string.Equals (HostOS, "Darwin", StringComparison.OrdinalIgnoreCase)) {
82102
var brew = Program.GetMetadata ("Homebrew");
83103
if (!string.IsNullOrEmpty (brew)) {
84104
InstallCommand = $"{Sudo}brew install '{brew}'";

build-tools/xa-prep-tasks/Xamarin.Android.BuildTools.PrepTasks/Which.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,17 @@ bool NeedInstall ()
142142

143143
Version GetCurrentVersion ()
144144
{
145-
string shell, format;
146-
GetShell (out shell, out format);
147-
148145
var command = GetHostProperty (Program, "CurrentVersionCommand", HostOS, HostOSName)
149146
?? Program.ItemSpec + " --version";
147+
148+
return GetProgramVersion (HostOS, command);
149+
}
150+
151+
internal static Version GetProgramVersion (string hostOS, string command)
152+
{
153+
string shell, format;
154+
GetShell (hostOS, out shell, out format);
155+
150156
var psi = new ProcessStartInfo (shell, string.Format (format, command)) {
151157
CreateNoWindow = true,
152158
UseShellExecute = false,
@@ -171,9 +177,9 @@ Version GetCurrentVersion ()
171177
: new Version (curVersion);
172178
}
173179

174-
void GetShell (out string shell, out string format)
180+
static void GetShell (string hostOS, out string shell, out string format)
175181
{
176-
if (HostOS == "Windows") {
182+
if (string.Equals (hostOS, "Windows", StringComparison.OrdinalIgnoreCase)) {
177183
shell = "cmd.exe";
178184
format = "/c \"{0}\"";
179185
return;

0 commit comments

Comments
 (0)