Skip to content

Commit b529f57

Browse files
committed
Fixed issue when patching projects without sources at all
1 parent 944382f commit b529f57

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/tctools/patch_plc/patch_plc_class.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,18 @@ def get_project_sources(self, tree) -> FileItems:
373373
# In the XML there are always backslashes, force to native type
374374
ref_set.add(path)
375375

376+
if self._element_files is None: # Group doesn't yet exist, create it:
377+
# It must be the first of the <ItemGroup> set
378+
element_neighbour: Element = tree.find(
379+
"PropertyGroup", namespaces={"": "*"}
380+
)
381+
self._element_files = etree.XML("<ItemGroup></ItemGroup>")
382+
element_neighbour.addnext(self._element_files)
383+
384+
if self._element_folders is None:
385+
self._element_folders = etree.XML("<ItemGroup></ItemGroup>")
386+
self._element_files.addnext(self._element_folders)
387+
376388
return sources
377389

378390
def sources_to_remove(
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<PropertyGroup>
3+
<FileVersion>1.0.0.0</FileVersion>
4+
<SchemaVersion>2.0</SchemaVersion>
5+
<ProjectGuid>{3863e7e1-422f-4d5c-92f7-9f7ce21f2ff0}</ProjectGuid>
6+
<SubObjectsSortedByName>True</SubObjectsSortedByName>
7+
<DownloadApplicationInfo>true</DownloadApplicationInfo>
8+
<WriteProductVersion>true</WriteProductVersion>
9+
<GenerateTpy>false</GenerateTpy>
10+
<Name>MyPlc</Name>
11+
<ProgramVersion>3.1.4023.0</ProgramVersion>
12+
<Application>{9b80525c-51bc-47f5-868f-9f61efadf857}</Application>
13+
<TypeSystem>{8c1ac7e2-784c-4117-8c69-6df5131a9b7a}</TypeSystem>
14+
<Implicit_Task_Info>{4cbef591-f0aa-4f71-8e8c-8748ed72f381}</Implicit_Task_Info>
15+
<Implicit_KindOfTask>{bc222fda-9a00-499c-8fe1-56590e05c741}</Implicit_KindOfTask>
16+
<Implicit_Jitter_Distribution>{85a038e2-e5b4-4af4-916e-a04812cc85b0}</Implicit_Jitter_Distribution>
17+
<LibraryReferences>{387c9843-650a-4728-bc5d-67105fea5b12}</LibraryReferences>
18+
</PropertyGroup>
19+
<ItemGroup>
20+
<PlaceholderReference Include="Tc2_Standard">
21+
<DefaultResolution>Tc2_Standard, * (Beckhoff Automation GmbH)</DefaultResolution>
22+
<Namespace>Tc2_Standard</Namespace>
23+
</PlaceholderReference>
24+
<PlaceholderReference Include="Tc2_System">
25+
<DefaultResolution>Tc2_System, * (Beckhoff Automation GmbH)</DefaultResolution>
26+
<Namespace>Tc2_System</Namespace>
27+
</PlaceholderReference>
28+
<PlaceholderReference Include="Tc3_Module">
29+
<DefaultResolution>Tc3_Module, * (Beckhoff Automation GmbH)</DefaultResolution>
30+
<Namespace>Tc3_Module</Namespace>
31+
</PlaceholderReference>
32+
</ItemGroup>
33+
<ItemGroup>
34+
<None Include="MyPlc.tmc">
35+
<SubType>Content</SubType>
36+
</None>
37+
</ItemGroup>
38+
<ProjectExtensions>
39+
<PlcProjectOptions>
40+
<XmlArchive>
41+
<Data>
42+
<o xml:space="preserve" t="OptionKey">
43+
<v n="Name">"&lt;ProjectRoot&gt;"</v>
44+
<d n="SubKeys" t="Hashtable" />
45+
<d n="Values" t="Hashtable" />
46+
</o>
47+
</Data>
48+
<TypeList>
49+
<Type n="Hashtable">System.Collections.Hashtable</Type>
50+
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
51+
<Type n="String">System.String</Type>
52+
</TypeList>
53+
</XmlArchive>
54+
</PlcProjectOptions>
55+
</ProjectExtensions>
56+
</Project>

tests/test_patch_plc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,14 @@ def test_reset_duplicate(plc_dir, project, caplog):
251251
assert "Refusing to add" in msg and "MAIN.TcPOU" in msg
252252

253253
assert project.read_text() == content_before # The project should not be changed
254+
255+
256+
def test_reset_empty_project(plc_dir, caplog):
257+
project = plc_dir / "MyPlc_empty.plcproj" # Project with 0 sources
258+
259+
code = PatchPlc(str(project), "reset", str(plc_dir), "-r").run()
260+
assert code == 0
261+
262+
content_after = project.read_text()
263+
assert 'Include="POUs\\untracked_source\\F_UntrackedFunc.TcPOU"' in content_after
264+
assert 'Include="POUs\\untracked_source"' in content_after

0 commit comments

Comments
 (0)