Added filename(); set solution or project file name independently of the solution or project name

This commit is contained in:
Jason Perkins 2012-10-24 15:40:25 -04:00
parent 8a6bb2c837
commit 1747035050
17 changed files with 232 additions and 126 deletions

View File

@ -20,11 +20,11 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.workspace", premake.codeblocks.workspace) premake.generate(sln, ".workspace", premake.codeblocks.workspace)
end, end,
onproject = function(prj) onproject = function(prj)
premake.generate(prj, "%%.cbp", premake.codeblocks.cbp) premake.generate(prj, ".cbp", premake.codeblocks.cbp)
end, end,
oncleansolution = function(sln) oncleansolution = function(sln)

View File

@ -20,11 +20,11 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.workspace", premake.codelite.workspace) premake.generate(sln, ".workspace", premake.codelite.workspace)
end, end,
onproject = function(prj) onproject = function(prj)
premake.generate(prj, "%%.project", premake.codelite.project) premake.generate(prj, ".project", premake.codelite.project)
end, end,
oncleansolution = function(sln) oncleansolution = function(sln)

View File

@ -61,14 +61,14 @@
-- function that will actually do the work of generating the file contents. -- function that will actually do the work of generating the file contents.
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln.txt", premake.example.solution) premake.generate(sln, ".sln.txt", premake.example.solution)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csprj.txt", premake.example.project) premake.generate(prj, ".csprj.txt", premake.example.project)
else else
premake.generate(prj, "%%.cprj.txt", premake.example.project) premake.generate(prj, ".cprj.txt", premake.example.project)
end end
end, end,

View File

@ -33,16 +33,16 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2005.generate_ng) premake.generate(sln, ".sln", vstudio.sln2005.generate_ng)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2005.generate_ng) premake.generate(prj, ".csproj", vstudio.cs2005.generate_ng)
premake.generate(prj, "%%.csproj.user", vstudio.cs2005.generate_user_ng) premake.generate(prj, ".csproj.user", vstudio.cs2005.generate_user_ng)
else else
premake.generate(prj, "%%.vcproj", vstudio.vc200x.generate_ng) premake.generate(prj, ".vcproj", vstudio.vc200x.generate_ng)
premake.generate(prj, "%%.vcproj.user", vstudio.vc200x.generate_user_ng) premake.generate(prj, ".vcproj.user", vstudio.vc200x.generate_user_ng)
end end
end, end,
@ -74,16 +74,16 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2005.generate_ng) premake.generate(sln, ".sln", vstudio.sln2005.generate_ng)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2005.generate_ng) premake.generate(prj, ".csproj", vstudio.cs2005.generate_ng)
premake.generate(prj, "%%.csproj.user", vstudio.cs2005.generate_user_ng) premake.generate(prj, ".csproj.user", vstudio.cs2005.generate_user_ng)
else else
premake.generate(prj, "%%.vcproj", vstudio.vc200x.generate_ng) premake.generate(prj, ".vcproj", vstudio.vc200x.generate_ng)
premake.generate(prj, "%%.vcproj.user", vstudio.vc200x.generate_user_ng) premake.generate(prj, ".vcproj.user", vstudio.vc200x.generate_user_ng)
end end
end, end,
@ -115,17 +115,17 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2005.generate_ng) premake.generate(sln, ".sln", vstudio.sln2005.generate_ng)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2005.generate_ng) premake.generate(prj, ".csproj", vstudio.cs2005.generate_ng)
premake.generate(prj, "%%.csproj.user", vstudio.cs2005.generate_user_ng) premake.generate(prj, ".csproj.user", vstudio.cs2005.generate_user_ng)
else else
premake.generate(prj, "%%.vcxproj", vstudio.vc2010.generate_ng) premake.generate(prj, ".vcxproj", vstudio.vc2010.generate_ng)
premake.generate(prj, "%%.vcxproj.user", vstudio.vc2010.generate_user_ng) premake.generate(prj, ".vcxproj.user", vstudio.vc2010.generate_user_ng)
premake.generate(prj, "%%.vcxproj.filters", vstudio.vc2010.generate_filters_ng) premake.generate(prj, ".vcxproj.filters", vstudio.vc2010.generate_filters_ng)
end end
end, end,
@ -158,17 +158,17 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2005.generate_ng) premake.generate(sln, ".sln", vstudio.sln2005.generate_ng)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2005.generate_ng) premake.generate(prj, ".csproj", vstudio.cs2005.generate_ng)
premake.generate(prj, "%%.csproj.user", vstudio.cs2005.generate_user_ng) premake.generate(prj, ".csproj.user", vstudio.cs2005.generate_user_ng)
else else
premake.generate(prj, "%%.vcxproj", vstudio.vc2010.generate_ng) premake.generate(prj, ".vcxproj", vstudio.vc2010.generate_ng)
premake.generate(prj, "%%.vcxproj.user", vstudio.vc2010.generate_user_ng) premake.generate(prj, ".vcxproj.user", vstudio.vc2010.generate_user_ng)
premake.generate(prj, "%%.vcxproj.filters", vstudio.vc2010.generate_filters_ng) premake.generate(prj, ".vcxproj.filters", vstudio.vc2010.generate_filters_ng)
end end
end, end,
@ -246,7 +246,7 @@
extension = iif(_ACTION > "vs2008", ".vcxproj", ".vcproj") extension = iif(_ACTION > "vs2008", ".vcxproj", ".vcproj")
end end
return premake.project.getfilename(prj, "%%" .. extension) return project.getfilename(prj, extension)
end end
@ -448,7 +448,7 @@
end end
function vstudio.cleanproject(prj) function vstudio.cleanproject(prj)
local fname = premake.project.getfilename(prj, "%%") local fname = project.getfilename(prj)
os.remove(fname .. ".vcproj") os.remove(fname .. ".vcproj")
os.remove(fname .. ".vcproj.user") os.remove(fname .. ".vcproj.user")
@ -524,16 +524,16 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2002.generate) premake.generate(sln, ".sln", vstudio.sln2002.generate)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2002.generate) premake.generate(prj, ".csproj", vstudio.cs2002.generate)
premake.generate(prj, "%%.csproj.user", vstudio.cs2002.generate_user) premake.generate(prj, ".csproj.user", vstudio.cs2002.generate_user)
else else
premake.generate(prj, "%%.vcproj", vstudio.vc200x.generate) premake.generate(prj, ".vcproj", vstudio.vc200x.generate)
premake.generate(prj, "%%.vcproj.user", vstudio.vc200x.generate_user) premake.generate(prj, ".vcproj.user", vstudio.vc200x.generate_user)
end end
end, end,
@ -563,16 +563,16 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2003.generate) premake.generate(sln, ".sln", vstudio.sln2003.generate)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2002.generate) premake.generate(prj, ".csproj", vstudio.cs2002.generate)
premake.generate(prj, "%%.csproj.user", vstudio.cs2002.generate_user) premake.generate(prj, ".csproj.user", vstudio.cs2002.generate_user)
else else
premake.generate(prj, "%%.vcproj", vstudio.vc200x.generate) premake.generate(prj, ".vcproj", vstudio.vc200x.generate)
premake.generate(prj, "%%.vcproj.user", vstudio.vc200x.generate_user) premake.generate(prj, ".vcproj.user", vstudio.vc200x.generate_user)
end end
end, end,
@ -602,16 +602,16 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2005.generate) premake.generate(sln, ".sln", vstudio.sln2005.generate)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2005.generate) premake.generate(prj, ".csproj", vstudio.cs2005.generate)
premake.generate(prj, "%%.csproj.user", vstudio.cs2005.generate_user) premake.generate(prj, ".csproj.user", vstudio.cs2005.generate_user)
else else
premake.generate(prj, "%%.vcproj", vstudio.vc200x.generate) premake.generate(prj, ".vcproj", vstudio.vc200x.generate)
premake.generate(prj, "%%.vcproj.user", vstudio.vc200x.generate_user) premake.generate(prj, ".vcproj.user", vstudio.vc200x.generate_user)
end end
end, end,
@ -641,16 +641,16 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2005.generate) premake.generate(sln, ".sln", vstudio.sln2005.generate)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2005.generate) premake.generate(prj, ".csproj", vstudio.cs2005.generate)
premake.generate(prj, "%%.csproj.user", vstudio.cs2005.generate_user) premake.generate(prj, ".csproj.user", vstudio.cs2005.generate_user)
else else
premake.generate(prj, "%%.vcproj", vstudio.vc200x.generate) premake.generate(prj, ".vcproj", vstudio.vc200x.generate)
premake.generate(prj, "%%.vcproj.user", vstudio.vc200x.generate_user) premake.generate(prj, ".vcproj.user", vstudio.vc200x.generate_user)
end end
end, end,
@ -681,17 +681,17 @@
}, },
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.sln", vstudio.sln2005.generate) premake.generate(sln, ".sln", vstudio.sln2005.generate)
end, end,
onproject = function(prj) onproject = function(prj)
if premake.isdotnetproject(prj) then if premake.isdotnetproject(prj) then
premake.generate(prj, "%%.csproj", vstudio.cs2005.generate) premake.generate(prj, ".csproj", vstudio.cs2005.generate)
premake.generate(prj, "%%.csproj.user", vstudio.cs2005.generate_user) premake.generate(prj, ".csproj.user", vstudio.cs2005.generate_user)
else else
premake.generate(prj, "%%.vcxproj", premake.vs2010_vcxproj) premake.generate(prj, ".vcxproj", premake.vs2010_vcxproj)
premake.generate(prj, "%%.vcxproj.user", premake.vs2010_vcxproj_user) premake.generate(prj, ".vcxproj.user", premake.vs2010_vcxproj_user)
premake.generate(prj, "%%.vcxproj.filters", vstudio.vc2010.generate_filters) premake.generate(prj, ".vcxproj.filters", vstudio.vc2010.generate_filters)
end end
end, end,

View File

@ -38,7 +38,7 @@
end, end,
onproject = function(prj) onproject = function(prj)
premake.generate(prj, "%%.xcodeproj/project.pbxproj", premake.xcode.project) premake.generate(prj, ".xcodeproj/project.pbxproj", premake.xcode.project)
end, end,
oncleanproject = function(prj) oncleanproject = function(prj)
@ -84,11 +84,11 @@
default_platform = "Universal", default_platform = "Universal",
onsolution = function(sln) onsolution = function(sln)
premake.generate(sln, "%%.xcworkspace/contents.xcworkspacedata", premake.xcode4.workspace_generate) premake.generate(sln, ".xcworkspace/contents.xcworkspacedata", premake.xcode4.workspace_generate)
end, end,
onproject = function(prj) onproject = function(prj)
premake.generate(prj, "%%.xcodeproj/project.pbxproj", premake.xcode.project) premake.generate(prj, ".xcodeproj/project.pbxproj", premake.xcode.project)
end, end,
oncleanproject = function(prj) oncleanproject = function(prj)

View File

@ -503,6 +503,13 @@
tokens = true, tokens = true,
} }
api.register {
name = "filename",
scope = "project",
kind = "string",
tokens = true,
}
api.register { api.register {
name = "files", name = "files",
scope = "config", scope = "config",
@ -1210,7 +1217,7 @@
local function createproject(name, sln, isUsage) local function createproject(name, sln, isUsage)
local prj = {} local prj = {}
-- attach a type -- attach a type
setmetatable(prj, { setmetatable(prj, {
__type = "project", __type = "project",
@ -1240,6 +1247,7 @@
prj.solution = sln prj.solution = sln
prj.name = name prj.name = name
prj.filename = name
prj.basedir = os.getcwd() prj.basedir = os.getcwd()
prj.script = _SCRIPT prj.script = _SCRIPT
prj.uuid = os.uuid() prj.uuid = os.uuid()
@ -1330,7 +1338,9 @@
premake.CurrentContainer = premake.solution.get(name) premake.CurrentContainer = premake.solution.get(name)
if (not premake.CurrentContainer) then if (not premake.CurrentContainer) then
premake.CurrentContainer = premake.solution.new(name) local sln = premake.solution.new(name)
sln.filename = name
premake.CurrentContainer = sln
end end
-- add an empty, global configuration -- add an empty, global configuration
@ -1349,13 +1359,8 @@
-- --
function external(name) function external(name)
-- define it like a regular project
local prj = project(name) local prj = project(name)
-- then mark it as external
prj.external = true; prj.external = true;
prj.externalname = prj.name
return prj return prj
end end

View File

@ -34,19 +34,18 @@
-- --
-- @param obj -- @param obj
-- A solution or project object; will be based to the callback function. -- A solution or project object; will be based to the callback function.
-- @param filename -- @param ext
-- The output filename; see the docs for premake.project.getfilename() -- An optional extension for the generated file, with the leading dot.
-- for the expected format.
-- @param callback -- @param callback
-- The function responsible for writing the file, should take a solution -- The function responsible for writing the file, should take a solution
-- or project as a parameters. -- or project as a parameters.
-- --
function premake.generate(obj, filename, callback) function premake.generate(obj, ext, callback)
filename = premake.project.getfilename(obj, filename) local fn = premake5.project.getfilename(obj, ext)
printf("Generating %s...", filename) printf("Generating %s...", path.getrelative(os.getcwd(), fn))
local f, err = io.open(filename, "wb") local f, err = io.open(fn, "wb")
if (not f) then if (not f) then
error(err, 0) error(err, 0)
end end

View File

@ -267,7 +267,7 @@
-- --
function premake.project.getfilename(prj, pattern) function premake.project.getfilename(prj, pattern)
local fname = pattern:gsub("%%%%", prj.externalname or prj.name) local fname = pattern:gsub("%%%%", prj.filename or prj.name)
fname = path.join(premake5.project.getlocation(prj), fname) fname = path.join(premake5.project.getlocation(prj), fname)
return path.getrelative(os.getcwd(), fname) return path.getrelative(os.getcwd(), fname)
end end

View File

@ -319,18 +319,34 @@
end end
--
-- Returns the file name for this solution.
--
-- @param sln
-- The solution object to query.
-- @param ext
-- An optional file extension to add, with the leading dot.
-- @return
-- The absolute path to the solution's file.
--
solution.getfilename = project.getfilename
-- --
-- Retrieve the solution's file system location. -- Retrieve the solution's file system location.
-- --
-- @param sln -- @param sln
-- The solution object to query. -- The solution object to query.
-- @param relativeto
-- Optional; if supplied, the location will be made relative
-- to this path.
-- @return -- @return
-- The path to the solutions's file system location. -- The path to the solution's file system location.
-- --
function solution.getlocation(sln) solution.getlocation = project.getlocation
return sln.location or sln.basedir
end
-- --

View File

@ -254,7 +254,7 @@
-- if this is an external project reference, I can't return -- if this is an external project reference, I can't return
-- any kind of path info, because I don't know the target name -- any kind of path info, because I don't know the target name
elseif not prj.externalname then elseif not prj.external then
if part == "basename" then if part == "basename" then
item = prjcfg.linktarget.basename item = prjcfg.linktarget.basename
else else

View File

@ -300,6 +300,24 @@
end end
--
-- Returns the file name for this project. Also works with solutions.
--
-- @param prj
-- The project object to query.
-- @param ext
-- An optional file extension to add, with the leading dot.
-- @return
-- The absolute path to the project's file.
--
function project.getfilename(prj, ext)
local fn = path.join(project.getlocation(prj), prj.filename)
if ext then fn = fn .. ext end
return fn
end
-- --
-- Returns a unique object file name for a project source code file. -- Returns a unique object file name for a project source code file.
-- --
@ -357,7 +375,7 @@
-- --
-- Retrieve the project's file system location. -- Retrieve the project's file system location. Also works with solutions.
-- --
-- @param prj -- @param prj
-- The project object to query. -- The project object to query.
@ -369,7 +387,13 @@
-- --
function project.getlocation(prj, relativeto) function project.getlocation(prj, relativeto)
local location = prj.location or prj.solution.location or prj.basedir local location = prj.location
if not location and prj.solution then
location = prj.solution.location
end
if not location then
location = prj.basedir
end
if relativeto then if relativeto then
location = path.getrelative(relativeto, location) location = path.getrelative(relativeto, location)
end end

View File

@ -217,7 +217,7 @@
for _, link in ipairs(links) do for _, link in ipairs(links) do
-- skip external project references, since I have no way -- skip external project references, since I have no way
-- to know the actual output target path -- to know the actual output target path
if not link.project.externalname then if not link.project.external then
if link.kind == premake.STATICLIB then if link.kind == premake.STATICLIB then
-- Don't use "-l" flag when linking static libraries; instead use -- Don't use "-l" flag when linking static libraries; instead use
-- path/libname.a to avoid linking a shared library of the same -- path/libname.a to avoid linking a shared library of the same

View File

@ -58,7 +58,8 @@ EndProject
-- --
function suite.projectNamesAreEscaped() function suite.projectNamesAreEscaped()
prj.name = 'My "x64" Project'; prj.name = 'My "x64" Project'
filename ('My "x64" Project')
prepare() prepare()
test.capture [[ test.capture [[
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "My "x64" Project", "My "x64" Project.vcproj", "{AE61726D-187C-E440-BD07-2556188A6565}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "My "x64" Project", "My "x64" Project.vcproj", "{AE61726D-187C-E440-BD07-2556188A6565}"

View File

@ -67,6 +67,7 @@
dofile("test_project.lua") dofile("test_project.lua")
dofile("project/test_eachconfig.lua") dofile("project/test_eachconfig.lua")
dofile("project/test_filtering.lua") dofile("project/test_filtering.lua")
dofile("project/test_filename.lua")
dofile("project/test_getconfig.lua") dofile("project/test_getconfig.lua")
dofile("project/test_hasconfig.lua") dofile("project/test_hasconfig.lua")
dofile("project/test_vpaths.lua") dofile("project/test_vpaths.lua")

View File

@ -0,0 +1,89 @@
--
-- tests/project/test_filename.lua
-- Verify generation of project (and solution) filenames.
-- Copyright (c) 2008-2012 Jason Perkins and the Premake project
--
T.project_filename = {}
local suite = T.project_filename
local project = premake5.project
--
-- Setup
--
local sln
function suite.setup()
sln, prj = test.createsolution()
end
local function prepare()
prj = premake.solution.getproject_ng(sln, 1)
end
--
-- Should return name as an absolute path.
--
function suite.isAbsolutePath()
prepare()
test.isequal(os.getcwd(), path.getdirectory(project.getfilename(prj)))
end
--
-- Should use the project name, if no filename was specified.
--
function suite.isProjectName_onNoFilename()
prepare()
test.isequal("MyProject", path.getname(project.getfilename(prj)))
end
--
-- Should use filename, if set via API.
--
function suite.doesUseFilename()
filename "Howdy"
prepare()
test.isequal("Howdy", path.getname(project.getfilename(prj)))
end
--
-- Appends file extension, if supplied.
--
function suite.doesUseExtension()
prepare()
test.isequal(".xc", path.getextension(project.getfilename(prj, ".xc")))
end
--
-- Should also work with solutions.
--
function suite.worksWithSolution()
prepare()
test.isequal("MySolution", path.getname(project.getfilename(sln)))
end
--
-- Value should not propagate down to projects.
--
function suite.doesNotPropagate()
solution ("MySolution")
filename ("Howdy")
prepare()
test.isequal("MyProject", path.getname(project.getfilename(prj)))
end

View File

@ -5,43 +5,32 @@
-- --
T.premake = { } T.premake = {}
local suite = T.premake
-- --
-- premake.checktools() tests -- Setup
-- --
function T.premake.checktools_SetsDefaultTools() local sln, prj
_ACTION = "gmake" function suite.setup()
premake.checktools() sln = test.createsolution()
test.isequal("gcc", _OPTIONS.cc) location "MyLocation"
test.isequal("mono", _OPTIONS.dotnet) prj = premake.solution.getproject_ng(sln, 1)
end
function T.premake.checktools_Fails_OnToolMismatch()
_ACTION = "gmake"
_OPTIONS["cc"] = "xyz"
ok, err = premake.checktools()
test.isfalse( ok )
test.isequal("the GNU Make action does not support /cc=xyz (yet)", err)
end end
-- --
-- generate() tests -- generate() tests
-- --
function T.premake.generate_OpensCorrectFile() function suite.generate_OpensCorrectFile()
prj = { name = "MyProject", location = "MyLocation" } premake.generate(prj, ".prj", function () end)
premake.generate(prj, "%%.prj", function () end) test.openedfile(path.join(os.getcwd(), "MyLocation/MyProject.prj"))
test.openedfile("MyLocation/MyProject.prj")
end end
function T.premake.generate_ClosesFile() function T.premake.generate_ClosesFile()
prj = { name = "MyProject", location = "MyLocation" } premake.generate(prj, ".prj", function () end)
premake.generate(prj, "%%.prj", function () end)
test.closedfile(true) test.closedfile(true)
end end

View File

@ -34,24 +34,6 @@
end end
--
-- getfilename() tests
--
function T.project.getfilename_ReturnsRelativePath()
local prj = { name = "project", location = "location" }
local r = _project.getfilename(prj, path.join(os.getcwd(), "../filename"))
test.isequal("../filename", r)
end
function T.project.getfilename_PerformsSubstitutions()
local prj = { name = "project", location = "location" }
local r = _project.getfilename(prj, "%%.prj")
test.isequal("location/project.prj", r)
end
-- --
-- premake.getlinks() tests -- premake.getlinks() tests
-- --