diff options
Diffstat (limited to 'src/samples/Dtf/ManagedCA/SampleCAs.cs')
-rw-r--r-- | src/samples/Dtf/ManagedCA/SampleCAs.cs | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/samples/Dtf/ManagedCA/SampleCAs.cs b/src/samples/Dtf/ManagedCA/SampleCAs.cs new file mode 100644 index 00000000..645131c8 --- /dev/null +++ b/src/samples/Dtf/ManagedCA/SampleCAs.cs | |||
@@ -0,0 +1,127 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | namespace WixToolset.Dtf.Samples.ManagedCA | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | using System.IO; | ||
8 | using WixToolset.Dtf.WindowsInstaller; | ||
9 | |||
10 | public class SampleCAs | ||
11 | { | ||
12 | [CustomAction] | ||
13 | public static ActionResult SampleCA1(Session session) | ||
14 | { | ||
15 | using (Record msgRec = new Record(0)) | ||
16 | { | ||
17 | msgRec[0] = "Hello from SampleCA1!" + | ||
18 | "\r\nCLR version is v" + Environment.Version; | ||
19 | session.Message(InstallMessage.Info, msgRec); | ||
20 | session.Message(InstallMessage.User, msgRec); | ||
21 | } | ||
22 | |||
23 | session.Log("Testing summary info..."); | ||
24 | SummaryInfo summInfo = session.Database.SummaryInfo; | ||
25 | session.Log("MSI PackageCode = {0}", summInfo.RevisionNumber); | ||
26 | session.Log("MSI ModifyDate = {0}", summInfo.LastSaveTime); | ||
27 | |||
28 | string testProp = session["SampleCATest"]; | ||
29 | session.Log("Simple property test: [SampleCATest]={0}.", testProp); | ||
30 | |||
31 | session.Log("Testing subdirectory extraction..."); | ||
32 | string testFilePath = "testsub\\SampleCAs.cs"; | ||
33 | if (!File.Exists(testFilePath)) | ||
34 | { | ||
35 | session.Log("Subdirectory extraction failed. File not found: " + testFilePath); | ||
36 | return ActionResult.Failure; | ||
37 | } | ||
38 | else | ||
39 | { | ||
40 | session.Log("Found file extracted in subdirectory."); | ||
41 | } | ||
42 | |||
43 | session.Log("Testing record stream extraction..."); | ||
44 | string tempFile = null; | ||
45 | try | ||
46 | { | ||
47 | tempFile = Path.GetTempFileName(); | ||
48 | using (View binView = session.Database.OpenView( | ||
49 | "SELECT `Binary`.`Data` FROM `Binary`, `CustomAction` " + | ||
50 | "WHERE `CustomAction`.`Target` = 'SampleCA1' AND " + | ||
51 | "`CustomAction`.`Source` = `Binary`.`Name`")) | ||
52 | { | ||
53 | binView.Execute(); | ||
54 | using (Record binRec = binView.Fetch()) | ||
55 | { | ||
56 | binRec.GetStream(1, tempFile); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | session.Log("CA binary file size: {0}", new FileInfo(tempFile).Length); | ||
61 | string binFileVersion = Installer.GetFileVersion(tempFile); | ||
62 | session.Log("CA binary file version: {0}", binFileVersion); | ||
63 | } | ||
64 | finally | ||
65 | { | ||
66 | if (tempFile != null && File.Exists(tempFile)) | ||
67 | { | ||
68 | File.Delete(tempFile); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | session.Log("Testing record stream reading..."); | ||
73 | using (View binView2 = session.Database.OpenView("SELECT `Data` FROM `Binary` WHERE `Name` = 'TestData'")) | ||
74 | { | ||
75 | binView2.Execute(); | ||
76 | using (Record binRec2 = binView2.Fetch()) | ||
77 | { | ||
78 | Stream stream = binRec2.GetStream("Data"); | ||
79 | string testData = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd(); | ||
80 | session.Log("Test data: " + testData); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | session.Log("Listing components"); | ||
85 | using (View compView = session.Database.OpenView( | ||
86 | "SELECT `Component` FROM `Component`")) | ||
87 | { | ||
88 | compView.Execute(); | ||
89 | foreach (Record compRec in compView) | ||
90 | { | ||
91 | using (compRec) | ||
92 | { | ||
93 | session.Log("\t{0}", compRec["Component"]); | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | |||
98 | session.Log("Testing the ability to access an external MSI database..."); | ||
99 | string tempDbFile = Path.GetTempFileName(); | ||
100 | using (Database tempDb = new Database(tempDbFile, DatabaseOpenMode.CreateDirect)) | ||
101 | { | ||
102 | // Just create an empty database. | ||
103 | } | ||
104 | using (Database tempDb2 = new Database(tempDbFile)) | ||
105 | { | ||
106 | // See if we can open and query the database. | ||
107 | IList<string> tables = tempDb2.ExecuteStringQuery("SELECT `Name` FROM `_Tables`"); | ||
108 | session.Log("Found " + tables.Count + " tables in the newly created database."); | ||
109 | } | ||
110 | File.Delete(tempDbFile); | ||
111 | |||
112 | return ActionResult.Success; | ||
113 | } | ||
114 | |||
115 | [CustomAction("SampleCA2")] | ||
116 | public static ActionResult SampleCustomAction2(Session session) | ||
117 | { | ||
118 | using (Record msgRec = new Record(0)) | ||
119 | { | ||
120 | msgRec[0] = "Hello from SampleCA2!"; | ||
121 | session.Message(InstallMessage.Info, msgRec); | ||
122 | session.Message(InstallMessage.User, msgRec); | ||
123 | } | ||
124 | return ActionResult.UserExit; | ||
125 | } | ||
126 | } | ||
127 | } | ||