Repository navigation
Expand file tree
/
Copy pathcopy.lua
More file actions
130 lines (120 loc) · 3.94 KB
/
Copy pathcopy.lua
File metadata and controls
130 lines (120 loc) · 3.94 KB
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
package.path = package.path ..";3rd/json.lua/?.lua"
local fs = require 'bee.filesystem'
local OS = require 'bee.platform'.os
local json = require "json"
local function readall(filename)
local f <close> = assert(io.open(filename, 'rb'))
return f:read 'a'
end
local function getExtensionDirName(packageDir)
local package = assert(json.decode(readall(packageDir .. '/package.json')))
local publisher = package.publisher
local name = package.name
local version = package.version
if not publisher then
error 'Cannot found `publisher` in package.json.'
end
if not name then
error 'Cannot found `name` in package.json.'
end
if not version then
error 'Cannot found `version` in package.json.'
end
return ('%s.%s-%s'):format(publisher,name,version)
end
local function crtdll(path)
if path:find("api-ms-win-", 1, true) then
return true
end
if path:sub(1, 5) == "msvcp" then
return true
end
if path:sub(1, 9) == "vcruntime" then
return true
end
if path == "concrt140.dll" then
return true
end
if path == "ucrtbase.dll" then
return true
end
return false
end
local function copy_directory(from, to, filter)
fs.create_directories(to)
for fromfile in fs.pairs(from) do
if (not filter) or filter(fromfile) then
if fs.is_directory(fromfile) then
copy_directory(fromfile, to / fromfile:filename(), filter)
else
local filename = fromfile:filename()
if not crtdll(filename:string():lower()) then
print('copy', fromfile, to / filename)
end
if OS == "macos" then
-- must remove first, it will cause a code signature error
fs.remove(to / filename)
end
fs.copy_file(fromfile, to / filename, fs.copy_options.overwrite_existing)
end
end
end
end
local function what_arch()
if OS == "windows" then
if os.getenv "PROCESSOR_ARCHITECTURE" == "ARM64" then
return "arm64"
end
if os.getenv "PROCESSOR_ARCHITECTURE" == "AMD64" or os.getenv "PROCESSOR_ARCHITEW6432" == "AMD64" then
return "x64"
end
return "ia32"
end
local f <close> = assert(io.popen("uname -m", 'r'))
return f:read 'l':lower()
end
local function detectPlatform(extensionPath, extensionDirName)
local extensionDirPrefix = fs.path(extensionPath) / extensionDirName
local function guess(platform)
local extensionDir = extensionDirPrefix..platform
if fs.exists(extensionDir / "package.json") then
return extensionDir
end
end
local arch = what_arch()
if OS == "windows" then
local r = guess('-win32-'..arch)
if r then return r end
if arch == "x64" then
r = guess '-win32-ia32'
if r then return r end
end
elseif OS == "linux" then
if arch == "x86_64" then
local r = guess '-linux-x64'
if r then return r end
elseif arch == "aarch64" then
local r = guess '-linux-arm64'
if r then return r end
end
elseif OS == "macos" then
if arch == "arm64" then
local r = guess '-darwin-arm64'
if r then return r end
end
local r = guess '-darwin-x64'
if r then return r end
end
local r = guess ''
if r then return r end
error("`" .. extensionDirPrefix:string() .. "` is not installed.")
end
local packageDir,sourceDir,extensionPath = ...
local extensionDirName = getExtensionDirName(packageDir)
local extensionDir = detectPlatform(extensionPath, extensionDirName)
local sourceDir = fs.path(sourceDir)
copy_directory(sourceDir, extensionDir, function (path)
local ext = path:extension():string():lower()
return ext ~= '.log' and path ~= sourceDir / "tmp"
end)
print 'ok'