Repository: alloy-framework/alloy
Pull request #21 (2024-09-11 23:11:27)

Add path property to ProjectDirectory
It's the directory that will contain the project directory.


Patch:
diff --git a/packages/csharp/src/components/ProjectDirectory.tsx b/packages/csharp/src/components/ProjectDirectory.tsx
index 3a5476b..e68d51d 100644
--- a/packages/csharp/src/components/ProjectDirectory.tsx
+++ b/packages/csharp/src/components/ProjectDirectory.tsx
@@ -1,10 +1,24 @@
 import * as core from "@alloy-js/core";
+import { join } from "pathe";
 
+// properties for creating the project directory
 export interface ProjectDirectoryProps {
+  // the name of the project directory
   name: string;
+
+  // the semver of the project
   version: string;
+
+  // project description
   description: string;
+
+  // parent path where the project directory will be created
+  path: string;
+
+  // directory name for source files. defaults to "src"
   srcDir?: string;
+
+  // child components of the project
   children?: core.Children;
 }
 
@@ -14,19 +28,19 @@ export function ProjectDirectory(props: ProjectDirectoryProps) {
     props.srcDir = "src";
   }
 
-  return <>
-      <core.SourceFile path={props.name+".csproj"} filetype="xml">
-        {core.code`
-          <Project Sdk="Microsoft.NET.Sdk">
-            <PropertyGroup>
-              <Version>${props.version}</Version>
-              <Description>${props.description}</Description>
-            </PropertyGroup>
-          </Project>
-        `}
-      </core.SourceFile>
-      <core.SourceDirectory path={props.srcDir}>
-        {props.children}
-      </core.SourceDirectory>
-    </>;
+  return <core.SourceDirectory path={join(props.path, props.name)}>
+    <core.SourceFile path={props.name+".csproj"} filetype="xml">
+      {core.code`
+        <Project Sdk="Microsoft.NET.Sdk">
+          <PropertyGroup>
+            <Version>${props.version}</Version>
+            <Description>${props.description}</Description>
+          </PropertyGroup>
+        </Project>
+      `}
+    </core.SourceFile>
+    <core.SourceDirectory path={props.srcDir}>
+      {props.children}
+    </core.SourceDirectory>
+  </core.SourceDirectory>;
 }


Test patch:
diff --git a/packages/csharp/test/projectdirectory.test.tsx b/packages/csharp/test/projectdirectory.test.tsx
index a0d13d5..527614a 100644
--- a/packages/csharp/test/projectdirectory.test.tsx
+++ b/packages/csharp/test/projectdirectory.test.tsx
@@ -6,7 +6,7 @@ import * as csharp from "../src/index.js";
 it("defines a project directory file with multiple source files", () => {
   const res = core.render(
     <core.Output>
-      <csharp.ProjectDirectory name="TestProject" version="0.1.0" description="a test project">
+      <csharp.ProjectDirectory name="TestProject" path="~/projects" version="0.1.0" description="a test project">
         <csharp.Namespace name='TestCode'>
           <csharp.SourceFile path="Test1.cs">
             <csharp.Class accessModifier='public' name="TestClass1" />
@@ -19,8 +19,11 @@ it("defines a project directory file with multiple source files", () => {
     </core.Output>,
   );
 
-  expect(res.contents[0].path).equals("TestProject.csproj");
-  expect(res.contents[0].contents).toBe(coretest.d`
+  const projDir = res.contents[0] as core.OutputDirectory;
+
+  expect(projDir.path).equals("~/projects/TestProject");
+  expect(projDir.contents[0].path).equals("~/projects/TestProject/TestProject.csproj");
+  expect(projDir.contents[0].contents).toBe(coretest.d`
     <Project Sdk="Microsoft.NET.Sdk">
       <PropertyGroup>
         <Version>0.1.0</Version>
@@ -29,18 +32,18 @@ it("defines a project directory file with multiple source files", () => {
     </Project>
   `);
 
-  const src = res.contents[1] as core.OutputDirectory;
+  const srcDir = projDir.contents[1] as core.OutputDirectory;
 
-  expect(src.contents[0].path).equals("src/Test1.cs");
-  expect(src.contents[0].contents).toBe(coretest.d`
+  expect(srcDir.contents[0].path).equals("~/projects/TestProject/src/Test1.cs");
+  expect(srcDir.contents[0].contents).toBe(coretest.d`
     namespace TestCode
     {
       public class TestClass1;
     }
   `);
 
-  expect(src.contents[1].path).equals("src/Test2.cs");
-  expect(src.contents[1].contents).toBe(coretest.d`
+  expect(srcDir.contents[1].path).equals("~/projects/TestProject/src/Test2.cs");
+  expect(srcDir.contents[1].contents).toBe(coretest.d`
     namespace TestCode
     {
       public class TestClass2;