工具(1):使用lua生成vs2015的工程文件/将已有的C++文件生成vs2015工程文件

时间:2022-06-01 18:58:47

因为vs2013/vs2015没有自动加载已有项目文件的功能,有时候由于项目庞大需要手动加载好久;或者只能使用eclipse,source insight等一些工具,其实用惯了vs感觉还是挺好的;然后就写了一个脚本用于vs2015自动加载目录下载的C++文件到工程:

ps:代码赶着写的比较娄,将就一下吧~

ps:使用的时候需要安装一下lua工具

   可以在Luaforge上找到编译好的Lua lib/dll/exe

        windows下最好是使用Lua for windows http://luaforge.net/projects/luaforwindows/,里面会包含一些常用的模块,比如luasocket,luasql,sqlite等。而且还包含了一个编辑器Scite,可以通过它来对lua脚本进行简单的调试,不需要额外安装什么IDE了

windows下最好是使用Lua for windows http://luaforge.net/projects/luaforwindows/,里面会包含一些常用的模块,比如luasocket,luasql,sqlite等。而且还包含了一个编辑器Scite,可以通过它来对lua脚本进行简单的调试,不需要额外安装什么IDE了

        如果你要自己编译Lua,可以在lua.org下载Lua的源代码,解压以后,参考readme文件,里面介绍的很详细。如果使用VC2008,也可以直接下载lua_vc2008.rar






package.path=package.path..";..\\?.lua;..\\..\\scripts\\?.lua"
local md5_core = require("md5")
local lfs = require"lfs"
-------------------------------------------------------------------------------------
-- sln文件
local objectFilename = "ws_activity"
-- 获取MD5
function getMd5(filename)
 local md5_str = md5_core.sum(filename)
 local md5_str_2 = string.gsub(md5_str, ".", 
function (c) 
return string.format("%02x", string.byte(c)) 
end
)
 local md5_str_3 = string.sub(md5_str_2, 0, 8).."-"..string.sub(md5_str_2, 9, 12)..
 "-"..string.sub(md5_str_2, 13, 16).."-"..string.sub(md5_str_2, 17, 20).."-"..string.sub(md5_str_2, 21, string.len(md5_str_2))
 return md5_str_3
 end
 
 ------------------------------------------------------------------------------------------------------------
local xiangmumd5 = getMd5(objectFilename) 
local SLNFILE1="\nMicrosoft Visual Studio Solution File, Format Version 12.00\
#Visual Studio 14\
VisualStudioVersion = 14.0.23107.0\
MinimumVisualStudioVersion = 10.0.40219.1\
Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""..objectFilename.."\", \"".. objectFilename ..".vcxproj\" , \"{"..xiangmumd5.."}\"\
EndProject \
Global\
GlobalSection(SolutionConfigurationPlatforms) = preSolution\
Debug|x64 = Debug|x64\
Debug|x86 = Debug|x86\
Release|x64 = Release|x64\
Release|x86 = Release|x86\
EndGlobalSection\
GlobalSection(ProjectConfigurationPlatforms) = postSolution\
{"..xiangmumd5.."}.Debug|x64.ActiveCfg = Debug|x64\
{"..xiangmumd5.."}.Debug|x64.Build.0 = Debug|x64\
{"..xiangmumd5.."}.Debug|x86.ActiveCfg = Debug|Win32\
{"..xiangmumd5.."}.Debug|x86.Build.0 = Debug|Win32\
{"..xiangmumd5.."}}.Release|x64.ActiveCfg = Release|x64\
{"..xiangmumd5.."}.Release|x64.Build.0 = Release|x64\
{"..xiangmumd5.."}.Release|x86.ActiveCfg = Release|Win32\
{"..xiangmumd5.."}.Release|x86.Build.0 = Release|Win32\
EndGlobalSection\
GlobalSection(SolutionProperties) = preSolution\
HideSolutionNode = FALSE\
EndGlobalSection\
EndGlobal\n"








function create(curPath)
   local filename = "<Filter Include=\""..curPath.."\">\
      <UniqueIdentifier>{"..getMd5(curPath).."}</UniqueIdentifier>\
    </Filter>\n"
return filename
end






-------------------------------------------------
-- vcxproj文件路径文件
local vcxprojfile1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<Project DefaultTargets=\"Build\" ToolsVersion=\"14.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\
  <ItemGroup Label=\"ProjectConfigurations\">\
    <ProjectConfiguration Include=\"Debug|Win32\">\
      <Configuration>Debug</Configuration>\
      <Platform>Win32</Platform>\
    </ProjectConfiguration>\
    <ProjectConfiguration Include=\"Release|Win32\">\
      <Configuration>Release</Configuration>\
      <Platform>Win32</Platform>\
    </ProjectConfiguration>\
    <ProjectConfiguration Include=\"Debug|x64\">\
      <Configuration>Debug</Configuration>\
      <Platform>x64</Platform>\
    </ProjectConfiguration>\
    <ProjectConfiguration Include=\"Release|x64\">\
      <Configuration>Release</Configuration>\
      <Platform>x64</Platform>\
    </ProjectConfiguration>\
  </ItemGroup>\
  <PropertyGroup Label=\"Globals\">\
    <ProjectGuid>{"..xiangmumd5.."}</ProjectGuid>\
    <RootNamespace>"..objectFilename.."</RootNamespace>\
    <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>\
  </PropertyGroup>\
  <Import Project=\"$(VCTargetsPath)\Microsoft.Cpp.Default.props\" />\
  <PropertyGroup Condition=\"\'$(Configuration)|$(Platform)\'==\'Debug|Win32\'\" Label=\"Configuration\">\
    <ConfigurationType>Application</ConfigurationType>\
    <UseDebugLibraries>true</UseDebugLibraries>\
    <PlatformToolset>v140</PlatformToolset>\
    <CharacterSet>Unicode</CharacterSet>\
  </PropertyGroup>\
  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\
    <ConfigurationType>Application</ConfigurationType>\
    <UseDebugLibraries>false</UseDebugLibraries>\
    <PlatformToolset>v140</PlatformToolset>\
    <WholeProgramOptimization>true</WholeProgramOptimization>\
    <CharacterSet>Unicode</CharacterSet>\
  </PropertyGroup>\
  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\">\
    <ConfigurationType>Application</ConfigurationType>\
    <UseDebugLibraries>true</UseDebugLibraries>\
    <PlatformToolset>v140</PlatformToolset>\
    <CharacterSet>Unicode</CharacterSet>\
  </PropertyGroup>\
  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\" Label=\"Configuration\">\
    <ConfigurationType>Application</ConfigurationType>\
    <UseDebugLibraries>false</UseDebugLibraries>\
    <PlatformToolset>v140</PlatformToolset>\
    <WholeProgramOptimization>true</WholeProgramOptimization>\
    <CharacterSet>Unicode</CharacterSet>\
  </PropertyGroup>\
  <Import Project=\"$(VCTargetsPath)\Microsoft.Cpp.props\" />\
  <ImportGroup Label=\"ExtensionSettings\">\
  </ImportGroup>\
  <ImportGroup Label=\"Shared\">\
  </ImportGroup>\
  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\
    <Import Project=\"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\
  </ImportGroup>\
  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\
    <Import Project=\"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\
  </ImportGroup>\
  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\
    <Import Project=\"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\
  </ImportGroup>\
  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\
    <Import Project=\"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\
  </ImportGroup>\
  <PropertyGroup Label=\"UserMacros\" />\
  <PropertyGroup />\
  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\
    <ClCompile>\
      <WarningLevel>Level3</WarningLevel>\
      <Optimization>Disabled</Optimization>\
      <SDLCheck>true</SDLCheck>\
    </ClCompile>\
    <Link>\
      <GenerateDebugInformation>true</GenerateDebugInformation>\
    </Link>\
  </ItemDefinitionGroup>\
  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\
    <ClCompile>\
      <WarningLevel>Level3</WarningLevel>\
      <Optimization>Disabled</Optimization>\
      <SDLCheck>true</SDLCheck>\
    </ClCompile>\
    <Link>\
      <GenerateDebugInformation>true</GenerateDebugInformation>\
    </Link>\
  </ItemDefinitionGroup>\
  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\
    <ClCompile>\
      <WarningLevel>Level3</WarningLevel>\
      <Optimization>MaxSpeed</Optimization>\
      <FunctionLevelLinking>true</FunctionLevelLinking>\
      <IntrinsicFunctions>true</IntrinsicFunctions>\
      <SDLCheck>true</SDLCheck>\
    </ClCompile>\
    <Link>\
      <GenerateDebugInformation>true</GenerateDebugInformation>\
      <EnableCOMDATFolding>true</EnableCOMDATFolding>\
      <OptimizeReferences>true</OptimizeReferences>\
    </Link>\
  </ItemDefinitionGroup>\
  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\
    <ClCompile>\
      <WarningLevel>Level3</WarningLevel>\
      <Optimization>MaxSpeed</Optimization>\
      <FunctionLevelLinking>true</FunctionLevelLinking>\
      <IntrinsicFunctions>true</IntrinsicFunctions>\
      <SDLCheck>true</SDLCheck>\
    </ClCompile>\
    <Link>\
      <GenerateDebugInformation>true</GenerateDebugInformation>\
      <EnableCOMDATFolding>true</EnableCOMDATFolding>\
      <OptimizeReferences>true</OptimizeReferences>\
    </Link>\
  </ItemDefinitionGroup>\
  <ItemGroup>\n"
  
  
 local vcxprojfile2 = " </ItemGroup>\
  <Import Project=\"$(VCTargetsPath)\Microsoft.Cpp.targets\" />\
  <ImportGroup Label=\"ExtensionTargets\">\
  </ImportGroup>\
</Project>"




------------------------------------------------------------------------------------------------
 local fileter_file1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\
  <ItemGroup>\n"


 
 
function createDirPath(curPath)
   local filename = "<Filter Include=\""..curPath.."\">\
      <UniqueIdentifier>{"..getMd5(curPath).."}</UniqueIdentifier>\
    </Filter>\n"
return filename
end










WSProject={}


--vcxproj文件中cpp的记录
local cppfile_v = nil
--vcxproj文件中h的记录
local hfile_v = nil


-- 用于记录文件夹的
 local dirfile_f = nil 
 -- 用于记录cpp文件的
 local cppfile_f = nil
 -- 用于记录h文件的
 local hfile_f = nil
-- 创建vcxproj文件:
function WSProject.loadAllFile(curpath)
  -- 创建文件夹路径
  dirfile_f = io.open("f1.txt", "w")
  cppfile_f = io.open("f2.txt", "w")
  hfile_f = io.open("f3.txt", "w") 
  cppfile_v = io.open("v1.txt", "w") 
  hfile_v = io.open("v2.txt", "w") 
    WSProject.GetAllXlsxFile(curpath)
  dirfile_f:close()
  cppfile_f:close()
  hfile_f:close()
  cppfile_v:close()
  hfile_v:close()
end






-- 获取目标路径下的所有.cpp文件和.h文件
function WSProject.GetAllXlsxFile(curpath)
   -- 循环遍历
  for ofile in lfs.dir(curpath)do
     if ofile ~= nil and ofile ~="." and ofile ~=".."then
       -- 获取文件属性
      local f = curpath..'\\'..ofile  
 -- 判断是文件夹还是文件
 local attr = lfs.attributes(f)
      assert (type(attr) == "table")
      if attr.mode == "directory" then
      -- 文件夹的处理方式
      local dirPath = string.sub(f,4,string.len(f))
  --dirfile
  local dirpath_1 = createDirPath(dirPath)
  dirfile_f:write(dirpath_1)
  
           WSProject.GetAllXlsxFile(f)
      else
      local dirPath_v = string.sub(curpath,4,string.len(curpath))
      -- 文件的处理方式
      -- 拆分文件及后缀
           local ext = f:match(".+%.(%w+)$")
  if ext == "cpp" then
    local cppfile_v_str = "<ClCompile Include=\""..f.."\" />\n"
    cppfile_v:write(cppfile_v_str)
local cppfile_f_str = "<ClCompile Include=\""..f.."\">\
 <Filter>"..dirPath_v.."</Filter>\
</ClCompile>\n"
    cppfile_f:write(cppfile_f_str)
  end
  if ext == "h" then
   local hfile_v_str = "<ClInclude Include=\""..f.."\" />\n"
   hfile_v:write(hfile_v_str)
local hfile_h_str = "<ClInclude Include=\""..f.."\">\
 <Filter>"..dirPath_v.."</Filter>\
</ClInclude>\n"
    hfile_f:write(hfile_h_str)
  end
 end
     end   
 end
end


-------------------------------------------------------------------------------------------------------------


-- 创建sln文件:
function WSProject.CreateSln()
    local slnfilename = objectFilename..".sln"
    local WriteOutFile = io.open(slnfilename, "w")
    if WriteOutFile ~= nil then 
WriteOutFile:write(SLNFILE1)
end  
    WriteOutFile:close()
print("create sln file success!")
end




-- 创建vcxproj文件
function WSProject.CreateVcxproj()
  local vfilename = objectFilename..".vcxproj"
  local WriteOutFile = io.open(vfilename, "w")
   WriteOutFile:write(vcxprojfile1)
   -- 读取cpp文件
     local read_cpp = io.open("v1.txt", "r")
     local line = read_cpp:read()
     while line do
  WriteOutFile:write("   "..line.."\n")
  line = read_cpp:read()
      end
 read_cpp:close()
     WriteOutFile:write(" </ItemGroup>\
 <ItemGroup>\n")
  -- 读取h文件
   local read_h = io.open("v2.txt", "r")
     local line_h = read_h:read()
     while line_h do
  WriteOutFile:write("   "..line_h.."\n")
  line_h = read_h:read()
      end
 read_h:close()
     WriteOutFile:write(vcxprojfile2)
  WriteOutFile:close()
  os.remove("v1.txt")
  os.remove("v2.txt")
end




-- 创建.vcxproj.filters文件
function WSProject.CreateFilters()
  local ffilename = objectFilename..".vcxproj.filters"
  local WriteOutFile = io.open(ffilename, "w")
   WriteOutFile:write(fileter_file1)
  
  -- 读取文件文件夹
     local read_dir = io.open("f1.txt", "r")
     local line_dir = read_dir:read()
     while line_dir do
  WriteOutFile:write("   "..line_dir.."\n")
  line_dir = read_dir:read()
      end
 read_dir:close()
     WriteOutFile:write(" </ItemGroup>\
 <ItemGroup>\n")
  -- 读取cpp文件
   local read_cpp = io.open("f2.txt", "r")
     local line_cpp = read_cpp:read()
     while line_cpp do
  WriteOutFile:write("   "..line_cpp.."\n")
  line_cpp = read_cpp:read()
      end
 read_cpp:close()
WriteOutFile:write(" </ItemGroup>\
 <ItemGroup>\n")
  -- 读取h文件
   local read_h = io.open("f3.txt", "r")
     local line_h = read_h:read()
     while line_h do
  WriteOutFile:write("   "..line_h.."\n")
  line_h = read_h:read()
      end
 read_h:close()
     WriteOutFile:write(" </ItemGroup>\n</Project>")
  WriteOutFile:close()
  os.remove("f1.txt")
  os.remove("f2.txt")
  os.remove("f3.txt")
end




----------------------------------------------------------------------------------------------------
WSProject.loadAllFile("..")
WSProject.CreateSln()
WSProject.CreateVcxproj()
WSProject.CreateFilters()