Skip to content

Commit bb78369

Browse files
committed
Add git integration.
1 parent a5cc8ed commit bb78369

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

src/_premake_init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,17 @@
564564
kind = "boolean"
565565
}
566566

567+
api.register {
568+
name = "gitintegration",
569+
scope = "global",
570+
kind = "string",
571+
allowed = {
572+
"Off",
573+
"Always",
574+
"OnNewFiles"
575+
}
576+
}
577+
567578
api.register {
568579
name = "inlining",
569580
scope = "config",

src/_premake_main.lua

+57
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,62 @@
333333
end
334334
end
335335

336+
---
337+
-- Run non-action generation part.
338+
---
339+
local function find_git_root(current_path)
340+
current_path = path.getabsolute(current_path or _MAIN_SCRIPT_DIR)
341+
342+
while not os.isdir(path.join(current_path, ".git")) do
343+
current_path = path.getdirectory(current_path)
344+
if current_path == "" then
345+
error("No git root path")
346+
end
347+
end
348+
return current_path
349+
end
350+
351+
local function print_git_post_checkout_hooks(root_path, mode)
352+
local args = {}
353+
for _, arg in ipairs(_ARGV) do
354+
if not (arg:startswith("--file") or arg:startswith("/file")) then
355+
table.insert(args, arg);
356+
end
357+
end
358+
359+
local indent = ''
360+
p.outln('#!bin/sh')
361+
if mode == 'OnNewFiles' then
362+
p.outln('count=`git diff --compact-summary $1 $2 | grep -E "( \\(new\\)| \\(gone\\)|premake)" | wc -l`')
363+
p.outln('if [ $count -ne 0 ]')
364+
p.outln('then')
365+
indent = ' '
366+
end
367+
p.outln(indent .. p.esc(path.getrelative(root_path, _PREMAKE_COMMAND)) .. ' --file=' .. p.esc(path.getrelative(root_path, _MAIN_SCRIPT)) .. ' ' .. table.concat(p.esc(args), ' '))
368+
if mode == 'OnNewFiles' then
369+
p.outln('fi')
370+
end
371+
end
372+
373+
function m.nonActionGeneration()
374+
local git_integration_mode = p.api.rootContainer().gitintegration or "Off"
375+
if git_integration_mode == "Off" then
376+
return
377+
end
378+
local root_path = find_git_root()
379+
380+
local content = p.capture(function () print_git_post_checkout_hooks(root_path, git_integration_mode) end)
381+
local res, err = os.writefile_ifnotequal(content, path.join(root_path, '.git', 'hooks', 'post-checkout'))
382+
383+
if (res == 0) then -- file not modified
384+
return
385+
elseif (res < 0) then
386+
error(err, 0)
387+
elseif (res > 0) then -- file modified
388+
printf("Generated %s...", path.getrelative(os.getcwd(), path.join(root_path, '.git', 'hooks', 'post-checkout')))
389+
return
390+
end
391+
end
336392

337393
---
338394
-- Override point, for logic that should run after validation and
@@ -387,6 +443,7 @@
387443
m.bake,
388444
m.postBake,
389445
m.validate,
446+
m.nonActionGeneration,
390447
m.preAction,
391448
m.callAction,
392449
m.postAction,

website/docs/gitintegration.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Enable git integration to run premake on checkout.
2+
3+
```lua
4+
gitintegration ("value")
5+
```
6+
7+
### Parameters ###
8+
9+
| Action | Description |
10+
|-----------------|----------------------------------------------------------------------------------|
11+
| Off | Disable git integration. |
12+
| Always | Run premake on checkout. |
13+
| OnNewFiles | Run premake only when files are added/removed or if premake script has changed. |
14+
15+
### Applies To ###
16+
17+
Global scope.
18+
19+
### Availability ###
20+
21+
Premake 5.0.0 beta 3 or later for Visual Studio 2019 and later.
22+
23+
### Examples ###
24+
25+
Regenerate autoversion.h with git tag when checkout to another branch.
26+
27+
```lua
28+
gitintegration "Always"
29+
30+
local locationDir = _OPTIONS["to"]
31+
32+
local function autoversion_h()
33+
local git_tag, errorCode = os.outputof("git describe --tag --always")
34+
if errorCode == 0 then
35+
print("git description: ", git_tag)
36+
local content = io.readfile("src/autoversion.h.in")
37+
content = content:gsub("${GIT_DESC}", git_tag)
38+
39+
os.mkdir(locationDir)
40+
local f, err = os.writefile_ifnotequal(content, path.join(locationDir, "autoversion.h"))
41+
42+
if (f == 0) then -- file not modified
43+
elseif (f < 0) then
44+
error(err, 0)
45+
return false
46+
elseif (f > 0) then
47+
print("Generated autoversion.h...")
48+
end
49+
50+
return true
51+
else
52+
print("`git describe --tag` failed with error code", errorCode, git_tag)
53+
return false
54+
end
55+
end
56+
57+
local have_autoversion_h = autoversion_h()
58+
59+
workspace "MyProject"
60+
location(locationDir)
61+
62+
if have_autoversion_h then
63+
includedirs { locationDir } -- for generated file (autoversion.h)
64+
end
65+
-- [..]
66+
```

website/sidebars.js

+8
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,14 @@ module.exports = {
307307
'xcodesystemcapabilities'
308308
]
309309
},
310+
{
311+
collapsed: true,
312+
type: 'category',
313+
label: 'Global Settings',
314+
items: [
315+
gitintegration
316+
]
317+
},
310318
{
311319
collapsed: true,
312320
type: 'category',

0 commit comments

Comments
 (0)