generate.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import pathlib
  3. import uuid
  4. REPOSITORY_ROOT = pathlib.Path(__file__).parent.parent.parent
  5. def generate(category, example_name, c_source_file):
  6. guid = str(uuid.uuid4()).upper()
  7. text = f"""
  8. <?xml version="1.0" encoding="utf-8"?>
  9. <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  10. <PropertyGroup Label="Globals">
  11. <ProjectGuid>{{{guid}}}</ProjectGuid>
  12. </PropertyGroup>
  13. <Import Project="$(VCTargetsPath)\\Microsoft.Cpp.Default.props" />
  14. <Import Project="$(VCTargetsPath)\\Microsoft.Cpp.props" />
  15. <ItemGroup>
  16. <None Include="$(SolutionDir)\\..\\examples\\{category}\\{example_name}\\README.txt" />
  17. <ClCompile Include="$(SolutionDir)\\..\\examples\\{category}\\{example_name}\\{c_source_file}" />
  18. </ItemGroup>
  19. <Import Project="$(VCTargetsPath)\\Microsoft.Cpp.targets" />
  20. </Project>
  21. """.strip()
  22. project_file = REPOSITORY_ROOT / "VisualC" / "examples" / category / example_name / f"{example_name}.vcxproj"
  23. if project_file.exists():
  24. print("Skipping:", project_file)
  25. return
  26. print("Generating file:", project_file)
  27. os.makedirs(project_file.parent, exist_ok=True)
  28. with open(project_file, "w", encoding="utf-8") as f:
  29. f.write(text)
  30. def get_c_source_filename(example_dir: pathlib.Path):
  31. """Gets the one and only C source file name in the directory of the example."""
  32. c_files = [f.name for f in example_dir.iterdir() if f.name.endswith(".c")]
  33. assert len(c_files) == 1
  34. return c_files[0]
  35. def main():
  36. path = REPOSITORY_ROOT / "examples"
  37. for category in path.iterdir():
  38. if category.is_dir():
  39. for example in category.iterdir():
  40. if example.is_dir():
  41. generate(category.name, example.name, get_c_source_filename(example))
  42. if __name__ == "__main__":
  43. main()