-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
170 lines (132 loc) · 3.74 KB
/
env.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package kirill_linux
import (
"fmt"
"github.com/kirillrdy/kirill-linux/config"
. "github.com/kirillrdy/kirill-linux/shell"
"io"
"log"
"net/http"
"os"
"path"
"runtime"
)
type Env struct {
// Cwd we remeber cwd in order to get back here
Cwd string
// DistfilesPath is where all distfiles are stored
DistfilesPath string
// BuildPath is where all the work is done
BuildPath string
// PkgPath is where binary packages get stored
PkgPath string
// InstallPrefix prefix is where we are all going to install things for new system
InstallPrefix string
}
func NewEnv() Env {
env := Env{}
env.setUpGlobals()
return env
}
func (env *Env) setUpGlobals() {
var err error
env.Cwd, err = os.Getwd()
crash(err)
hiddenDir := ".everything"
env.DistfilesPath = path.Join(env.Cwd, hiddenDir, "distfiles")
err = os.MkdirAll(env.DistfilesPath, os.ModePerm)
crash(err)
env.BuildPath = path.Join(env.Cwd, hiddenDir, "build")
err = os.MkdirAll(env.BuildPath, os.ModePerm)
crash(err)
env.PkgPath = path.Join(env.Cwd, hiddenDir, "package")
err = os.MkdirAll(env.PkgPath, os.ModePerm)
crash(err)
env.InstallPrefix = path.Join(env.Cwd, hiddenDir, "newroot")
err = os.MkdirAll(env.InstallPrefix, os.ModePerm)
crash(err)
env.appendToPath()
}
func (env Env) appendToPath() {
pathVar := os.Getenv("PATH")
//TODO How will this work for tcsh
os.Setenv("PATH", path.Join(env.InstallPrefix, "bin")+":"+pathVar)
}
// TODO Hmm see how to make "protected"
func (env Env) Exec(cmd string, args ...string) {
ExecInteractive(cmd, args...)
}
func (env Env) Shell(cmd string) {
shell := os.Getenv("SHELL")
env.Exec(shell, "-c", cmd)
}
func (env Env) InteractiveShell() {
shell := os.Getenv("SHELL")
//TODO also change shell prompt
env.Exec(shell)
}
func (env Env) fetch(url string) {
if config.Verbose {
log.Printf("fetching %s\n", url)
}
Cd(env.DistfilesPath)
// Get the data
resp, err := http.Get(url)
crash(err)
defer resp.Body.Close()
// Create the file
out, err := os.Create(path.Base(url))
crash(err)
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
crash(err)
Cd(env.Cwd)
}
func (env Env) extract(url string) {
expetedTarPath := path.Join(env.DistfilesPath, path.Base(url))
if _, err := os.Stat(expetedTarPath); os.IsNotExist(err) {
env.fetch(url)
}
extractedSourcePath := path.Join(env.BuildPath, packageVersion(url))
if _, err := os.Stat(extractedSourcePath); os.IsNotExist(err) {
tarballPath := path.Join(env.DistfilesPath, path.Base(url))
Tar("xf", tarballPath, "-C", env.BuildPath)
}
Cd(extractedSourcePath)
}
func NumberOfMakeJobs() string {
return fmt.Sprintf("-j%d", runtime.NumCPU())
}
func (env Env) ConfigureInstall(url string, configure func()) {
env.BuildInstall(url, func() {
configure()
Make(NumberOfMakeJobs())
}, func(destDir string) {
Make("install", "DESTDIR="+destDir)
})
}
// stupid name, but what can you do
func (env Env) BuildInstall(url string, build func(), install func(string)) {
tarBall := path.Join(env.PkgPath, packageVersion(url)+".tar.xz")
if _, err := os.Stat(tarBall); os.IsNotExist(err) {
env.extract(url)
sourceDir := path.Join(env.BuildPath, packageVersion(url))
build()
// Part of packaging
destDir := path.Join(env.BuildPath, packageVersion(url)+"-package")
install(destDir)
Tar("cf", tarBall, "-C", path.Join(destDir, env.InstallPrefix), ".")
Cd(env.Cwd)
Rm("-rf", destDir)
Rm("-rf", sourceDir)
}
//TODO also dont do this if its already installed eg need some way of tracking those
//TODO replace with desired prefix
Tar("xf", tarBall, "-C", env.InstallPrefix)
}
func (env Env) Install(url string) {
env.ConfigureInstall(url, func() {
//TODO think about how to restore this to --prefix=/usr
DotConfigure("--prefix=" + env.InstallPrefix)
})
}