C# 使用premake5将属性注入VS项目
我希望能够在premake生成的Visual Studio项目中设置一些任意属性,作为站点策略,而不是基于每个项目 这是一个有效的例子,但并不理想。这在我的premake5-system.lua启动文件中。它覆盖vs2015操作使用的项目生成器,从而有条件地覆盖项目属性生成函数C# 使用premake5将属性注入VS项目,c#,lua,premake,C#,Lua,Premake,我希望能够在premake生成的Visual Studio项目中设置一些任意属性,作为站点策略,而不是基于每个项目 这是一个有效的例子,但并不理想。这在我的premake5-system.lua启动文件中。它覆盖vs2015操作使用的项目生成器,从而有条件地覆盖项目属性生成函数 premake.override(premake.action._list.vs2015, 'onProject', function(base, prj) -- For C# libraries force
premake.override(premake.action._list.vs2015, 'onProject', function(base, prj)
-- For C# libraries force static code analysis on.
if premake.project.isdotnet(prj) and prj.kind == premake.SHAREDLIB then
premake.override(premake.vstudio.cs2005, "compilerProps", function(base, cfg)
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
base(cfg)
end)
end
base(prj)
end)
premake.override(premake.action.\u list.vs2015,'onProject',函数(base,prj)
--对于C#库,强制执行静态代码分析。
如果premake.project.isdotnet(prj)和prj.kind==premake.SHAREDLIB,那么
重写(premake.vstudio.cs2005,“编译器程序”,函数(base,cfg)
_p(2,‘真’)
基地(cfg)
(完)
结束
基地(prj)
(完)
我不喜欢这段代码明确提到VS2015,因为这意味着我必须在其他VS版本中复制它。我不喜欢它明确提到cs2005项目生成器,因为如果premake曾经停止在更高版本的VS中使用2005属性发射器,那么这将中断
这是否可以变得更通用,或者这是正确的方法
更新:
我发现在onProject()的重写中添加重写的方案是有缺陷的,因为如果工作区中有多个项目,内部重写将被添加多次,从而在某些项目中多次发出自定义属性。这是一个改进的版本,但我仍然想知道如何避免直接覆盖cs2005带来的脆弱性:
premake.override(premake.vstudio.cs2005, "compilerProps", function(base, cfg)
local prj = cfg.project
if premake.project.isdotnet(prj) then
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
end
base(cfg)
end)
premake.override(premake.vstudio.cs2005,“编译器程序”,函数(base,cfg)
本地prj=cfg.project
如果是premake.project.isdotnet(prj),那么
_p(2,‘真’)
结束
基地(cfg)
(完)
我认为正确的答案是创建一个新的项目API调用,在csproj.compilerProps
中使用新值,然后。这个特性没有什么争议,合并起来应该很容易……这样你就不必维护覆盖
类似于\u premake\u init.lua中的内容:
api.register {
name = "codeanalysis",
scope = "config",
kind = "boolean",
}
if cfg.codeanalysis then
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
end
m.elements.compilerProps = function(cfg)
return {
m.defineConstants,
m.errorReport,
m.runCodeAnalysis,
m.warningLevel,
m.allowUnsafeBlocks,
m.treatWarningsAsErrors,
m.commandLineParameters,
}
end)
function cs2005.compilerProps(cfg)
p.callArray(m.elements.compilerProps, cfg)
end
function m.defineConstants(cfg)
_x(2,'<DefineConstants>%s</DefineConstants>', table.concat(cfg.defines, ";"))
end
-- and so on…
这在vs2005\u csproj.lua中:
api.register {
name = "codeanalysis",
scope = "config",
kind = "boolean",
}
if cfg.codeanalysis then
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
end
m.elements.compilerProps = function(cfg)
return {
m.defineConstants,
m.errorReport,
m.runCodeAnalysis,
m.warningLevel,
m.allowUnsafeBlocks,
m.treatWarningsAsErrors,
m.commandLineParameters,
}
end)
function cs2005.compilerProps(cfg)
p.callArray(m.elements.compilerProps, cfg)
end
function m.defineConstants(cfg)
_x(2,'<DefineConstants>%s</DefineConstants>', table.concat(cfg.defines, ";"))
end
-- and so on…
为了获得额外积分,您可以重构csproj.compilerProps
,以使用新的、更可扩展的“调用数组”方法,如vs2010\u vcxproj.lua
:
api.register {
name = "codeanalysis",
scope = "config",
kind = "boolean",
}
if cfg.codeanalysis then
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
end
m.elements.compilerProps = function(cfg)
return {
m.defineConstants,
m.errorReport,
m.runCodeAnalysis,
m.warningLevel,
m.allowUnsafeBlocks,
m.treatWarningsAsErrors,
m.commandLineParameters,
}
end)
function cs2005.compilerProps(cfg)
p.callArray(m.elements.compilerProps, cfg)
end
function m.defineConstants(cfg)
_x(2,'<DefineConstants>%s</DefineConstants>', table.concat(cfg.defines, ";"))
end
-- and so on…
m.elements.compilerProps=函数(cfg)
返回{
m、 明确的情况,
m、 错误报告,
m、 运行码分析,
m、 警告级别,
m、 allowUnsafeBlocks,
m、 处理警告错误,
m、 命令行参数,
}
(完)
函数cs2005.compilerProps(cfg)
p、 callArray(m.elements.compilerProps,cfg)
结束
函数m.defineConstants(cfg)
_x(2,'%s',table.concat(cfg.defines,“;”))
结束
--等等…
我认为正确的答案是创建一个新的项目API调用,在csproj.compilerProps
中使用新值,然后。这个特性没有什么争议,合并起来应该很容易……这样你就不必维护覆盖
类似于\u premake\u init.lua中的内容:
api.register {
name = "codeanalysis",
scope = "config",
kind = "boolean",
}
if cfg.codeanalysis then
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
end
m.elements.compilerProps = function(cfg)
return {
m.defineConstants,
m.errorReport,
m.runCodeAnalysis,
m.warningLevel,
m.allowUnsafeBlocks,
m.treatWarningsAsErrors,
m.commandLineParameters,
}
end)
function cs2005.compilerProps(cfg)
p.callArray(m.elements.compilerProps, cfg)
end
function m.defineConstants(cfg)
_x(2,'<DefineConstants>%s</DefineConstants>', table.concat(cfg.defines, ";"))
end
-- and so on…
这在vs2005\u csproj.lua中:
api.register {
name = "codeanalysis",
scope = "config",
kind = "boolean",
}
if cfg.codeanalysis then
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
end
m.elements.compilerProps = function(cfg)
return {
m.defineConstants,
m.errorReport,
m.runCodeAnalysis,
m.warningLevel,
m.allowUnsafeBlocks,
m.treatWarningsAsErrors,
m.commandLineParameters,
}
end)
function cs2005.compilerProps(cfg)
p.callArray(m.elements.compilerProps, cfg)
end
function m.defineConstants(cfg)
_x(2,'<DefineConstants>%s</DefineConstants>', table.concat(cfg.defines, ";"))
end
-- and so on…
为了获得额外积分,您可以重构csproj.compilerProps
,以使用新的、更可扩展的“调用数组”方法,如vs2010\u vcxproj.lua
:
api.register {
name = "codeanalysis",
scope = "config",
kind = "boolean",
}
if cfg.codeanalysis then
_p(2, '<RunCodeAnalysis>true</RunCodeAnalysis>')
end
m.elements.compilerProps = function(cfg)
return {
m.defineConstants,
m.errorReport,
m.runCodeAnalysis,
m.warningLevel,
m.allowUnsafeBlocks,
m.treatWarningsAsErrors,
m.commandLineParameters,
}
end)
function cs2005.compilerProps(cfg)
p.callArray(m.elements.compilerProps, cfg)
end
function m.defineConstants(cfg)
_x(2,'<DefineConstants>%s</DefineConstants>', table.concat(cfg.defines, ";"))
end
-- and so on…
m.elements.compilerProps=函数(cfg)
返回{
m、 明确的情况,
m、 错误报告,
m、 运行码分析,
m、 警告级别,
m、 allowUnsafeBlocks,
m、 处理警告错误,
m、 命令行参数,
}
(完)
函数cs2005.compilerProps(cfg)
p、 callArray(m.elements.compilerProps,cfg)
结束
函数m.defineConstants(cfg)
_x(2,'%s',table.concat(cfg.defines,“;”))
结束
--等等…
这个想法在我脑海中闪过,但是“更改基础产品”感觉不是一种非常可扩展的定制方法,因为通常会有新的扩展向VS项目文件添加新属性。make和VS等构建系统之间的差异往往会通过提供这种包装来挫败规范化其接口的尝试。是否应该添加一种机制,将自定义属性作为键/值对插入?处理XML范围可能很困难,因为看起来premake是串行写入VS文件,而不是首先在内存中构建DOM。premake等工具的全部目的是提供这种包装器,并提供帮助。也就是说,如果compilerProps
按照所述进行了修改(不管最终会发生什么),那么您可以覆盖m.elements.compilerProps
,并在那里添加/删除新条目,以更改写入块中的内容。我曾想过这个想法,但“更改基本产品”当经常有新的扩展向VS项目文件中添加新属性时,感觉不是一种非常可伸缩的定制方法。make和VS等构建系统之间的差异往往会通过提供这种包装来挫败规范化其接口的尝试。是否应该添加一种机制,将自定义属性作为键/值对插入?处理XML范围可能很困难,因为看起来premake是串行写入VS文件,而不是首先在内存中构建DOM。premake等工具的全部目的是提供这种包装器,并提供帮助。也就是说,如果compilerProps
按照所述进行了修改(不管最终会发生什么),那么您可以覆盖m.elements.compilerProps
,并在那里添加/删除新条目,以更改写入块中的内容。