summaryrefslogtreecommitdiffstats
path: root/bin/ap.lua
blob: 1251064f713643747f1e6bf97b4e7776258a3cd0 (plain)
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
#!/usr/bin/lua5.2

local lfs = require('lfs')


-- subcommands -----------------------
local subcmd = {}

subcmd.revdep = {
	desc = "Print reverse dependencies",
	usage = "PKG...",
	run = function(db, opts)
		for i = 1, #opts do
			for _, pkg in db:each_reverse_dependency(opts[i]) do
				print(pkg.pkgname)
			end
		end
	end
}

subcmd.list = {
	desc = "Print all packages built from aports tree",
	usage = "",
	run = function(db)
		for _,pn in db:each() do
			print(pn)
		end
	end
}

subcmd.recursdeps = {
	desc = "Recursively print all make dependencies for given packages",
	usage = "PKG...",
	run = function (db, opts)
		for i = 1, #opts do
			for dep in db:recursive_dependencies(opts[i]) do
				print(dep)
			end
		end
	end
}

subcmd.builddirs = {
	desc = "Print the build dirs for given packages in build order",
	usage = "PKG...",
	run = function(db, opts)
		for pkg in db:each_in_build_order(opts) do
			print(pkg.dir)
		end
	end
}

subcmd.sources = {
	desc = "List sources",
	usage = "PKG...",
	run = function(db, opts)
		for i = 1, #opts do
			for pkg in db:each_pkg_with_name(opts[i]) do
				for url in pkg:remote_sources() do
					print(pkg.pkgname, pkg.pkgver, string.gsub(url, pkg.pkgver, "$VERSION"))
				end
			end
		end
	end
}

subcmd["build-list"] = {
	desc = "List packages that can/should be rebuilt",
	usage = "",
	run = function(db)
		local nlist = {}
		for pkg in db:each_need_build() do
			table.insert(nlist, pkg.pkgname)
		end
		for pkg in db:each_in_build_order(nlist) do
			print(pkg.dir)
		end
	end
}

subcmd["apk-list"] = {
	desc = "List all apk files",
	usage = "",
	run = function(db)
		for pkg in db:each() do
			if pkg:relevant() then
				print(pkg:get_apk_file_name())
			end
		end
	end
}

subcmd["dump-json"] = {
	desc = "Dump all abuilds from aports tree to JSON",
	run = function(db)
		local dump = require "aports.dump"
		print(dump.pkgs_to_json(db:each_aport()))
	end
}

local function print_usage()
	io.write("usage: ap -d <DIR> SUBCOMMAND [options]\n\nSubcommands are:\n")
	for k in pairs(subcmd) do
		print("  "..k)
	end
end

-- those should be read from some config file
local repodirs = {}


-- parse args
local opts = {}
local help = false
do
	local i = 1
	while i <= #arg do
		if arg[i] == "-d" then
			i = i + 1
			repodirs[#repodirs + 1] = arg[i]
		elseif arg[i] == "-h" then
			help = true
		else
			opts[#opts + 1] = arg[i]
		end
		i = i + 1
	end
end

local cmd = table.remove(opts, 1)

if help or not cmd then
	print_usage()
	-- usage
	return
end

if #repodirs == 0 then
	if lfs.attributes("APKBUILD") then
		repodirs[1] = string.gsub(lfs.currentdir(), "(.*)/.*", "%1")
	else
		repodirs[1] = lfs.currentdir()
	end
end

if subcmd[cmd] and type(subcmd[cmd].run) == "function" then
	for _, dir in pairs(repodirs) do
		local db = require('aports.db').new(dir:match("(.*)/([^/]*)"))
		subcmd[cmd].run(db, opts)
	end
else
	io.stderr:write(cmd..": invalid subcommand\n")
end