This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstaller.php
executable file
·142 lines (123 loc) · 4.75 KB
/
installer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/* Terminus Installer Script
Created by Alex Fornuto - [email protected]
*/
// Define environment variables.
$pathUpdated = false;
$paths = explode(":", getenv('PATH')); // Creates an array with all paths in $PATH
$installdir = ("/usr/local/bin"); // Creates a string with the desired installation path
$rcfiles = array( // Array of common .rc files to look for.
".bashrc",
".zshrc",
".config/fish/config.fish",
".profile",
);
$package = "terminus";
// Function to download Terminus executable file from GitHub to /tmp/ then move it to $installdir
// prompts for sudo if required.
function downloadTerminus($installdir, $package)
{
// $opts defines values required by the GitHub API to respond correctly. $context formats them for use.
$opts = [
'http' => [
'method' => 'GET',
'header' => [
'User-Agent: PHP'
]
]
];
$context = stream_context_create($opts);
$releases = file_get_contents("https://api.github.com/repos/pantheon-systems/" . $package . "/releases", false, $context);
$releases = json_decode($releases);
$version = $releases[0]->tag_name;
$url = $releases[0]->assets[0]->browser_download_url;
// Do the needful
echo("\nDownloading Terminus " . $version . " from " . $url . " to /tmp \n");
$couldDownload = file_put_contents("/tmp/" . $package . ".phar", file_get_contents($url));
echo("Moving to " . $installdir . "...\n");
if(!rename("/tmp/" . $package . ".phar", $installdir . "/" . $package)){
echo("\n" . $installdir . " requires admin rights to write to...\n");
exec("sudo mv /tmp/" . $package . ".phar " . $installdir . "/" . $package);
echo("\n");
}
// Return true if successful
$couldMove = exec("ls " . $installdir . "/" . $package, $output, $couldMove);
return $couldMove;
}
// Function to add to any common shell configuration files a line to amend $PATH with $installdir.
function ammendPath($rcfile, $installdir, &$pathUpdated)
{
$pathUpdated = file_put_contents(getenv('HOME') . "/$rcfile", "# Adds Terminus to \$PATH\nPATH=\$PATH:" . $installdir . "\n\n", FILE_APPEND | LOCK_EX);
if (!$pathUpdated) {
throw new Exception($rcfile . " found, but unable to write to it.");
}
return $pathUpdated;
}
function checkCompletion($rcfile)
{
if( strpos(file_get_contents(getenv('HOME') . "/$rcfile"), "eval \"$(terminus autocomplete)\"") !== false)
{
return false;
}
else {
return true;
}
}
function enableAutoComplete($rcfile)
{
$evalCompletion = file_put_contents(getenv('HOME') . "/$rcfile", "\n# Enable Terminus autocomplete\neval \"$(terminus autocomplete)\"", FILE_APPEND | LOCK_EX);
if (!$evalCompletion) {
throw new Exception($rcfile . " found, but unable to write to it.");
}
return $evalCompletion;
}
// Function to determine if ~/.terminus/bin is already in $PATH
function checkpath($paths, $installdir)
{
return in_array($installdir, $paths);
}
// BEGIN ACTUAL DOING OF THINGS!
//Makes ~/.terminus/bin if it doesn't exist.
if (!file_exists($installdir)) {
echo("Creating " . $installdir . "/\n");
mkdir($installdir, 0700, true);
}
//Download terminus.phar
if (downloadTerminus($installdir, $package)) {
echo("Installed to " . $installdir . "\n\n");
} else {
exit("Download unsuccessful.\n\n");
}
// Make Terminus executable
echo("Making Terminus executable... ");
chmod($installdir . "/" . $package, 0755)
or exit("\nUnable to set Terminus as executable.\n");
echo("Done.\n");
// If $installdir isn't in path, add it.
if (checkpath($paths, $installdir) === false) {
foreach ($rcfiles as $rcfile) {
if (file_exists(getenv('HOME') . "/$rcfile") && is_writable(getenv('HOME') . "/$rcfile")) {
ammendpath($rcfile, $installdir, $pathUpdated);
echo("Found " . $rcfile . " and added " . $installdir .
" to your \$PATH.\nIn order to run Terminus, you must first run:\n\nsource ~/" . $rcfile . "\n");
}
}
if (!$pathUpdated) {
echo("Terminus has been installed to " . $installdir .
" But no suitable configuration file was found to update \$PATH.\n\nYou must manually add " . $installdir .
" to your PATH, or execute Terminus from the full path.\n\n");
}
}
// Check for autocompletion in rcfile and amend if not
foreach ($rcfiles as $rcfile) {
if (file_exists(getenv('HOME') . "/$rcfile") && is_writable(getenv('HOME') . "/$rcfile")){
if (checkCompletion($rcfile) === true) {
enableAutoComplete($rcfile);
echo("Added Autocompletion to " . $rcfile . "\n");
}
else {
echo("Found autocompletion setting for Terminus in " . "/$rcfile" . "\n" );
}
}
}
exit();