Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trace example #24

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"LPSTR",
"msbuild",
"msica",
"msidb",
"msiexec",
"MSIHANDLE",
"msvc",
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ path = "examples/deferred/lib.rs"
name = "skip"
crate-type = ["cdylib"]
path = "examples/skip/lib.rs"

[[example]]
name = "trace"
crate-type = ["cdylib"]
path = "examples/trace/lib.rs"
1 change: 1 addition & 0 deletions examples/example.wixproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<Compile Include="example.wxs" />
<Compile Include="deferred\deferred.wxs" />
<Compile Include="skip\skip.wxs" />
<Compile Include="trace\trace.wxs" />
</ItemGroup>
<ItemGroup>
<BindInputPaths Include="..\target\$(Configuration)" />
Expand Down
1 change: 1 addition & 0 deletions examples/example.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
<!-- Examples -->
<CustomActionRef Id="DeferredExampleCustomAction" />
<CustomActionRef Id="SkipExampleCustomAction" />
<CustomActionRef Id="TraceExampleCustomAction" />
</Product>
</Wix>
4 changes: 4 additions & 0 deletions examples/skip/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub extern "C" fn SkipExampleCustomAction(session: Session) -> CustomActionResul
}
true => {
let data = session.property("CustomActionData")?;
if data.is_empty() {
return Succeed;
}

// Unnecessarily parsing the string demonstrates using ? for any possible error.
let data = data.parse::<u32>()?;
if data == 2 {
Expand Down
83 changes: 83 additions & 0 deletions examples/trace/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2022 Heath Stewart.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

use msica::CustomActionResult::Succeed;
use msica::*;

const MSIDB_CUSTOM_ACTION_TYPE_DLL: i32 = 1;
const MSIDB_CUSTOM_ACTION_TYPE_CONTINUE: i32 = 64;
const MSIDB_CUSTOM_ACTION_TYPE_IN_SCRIPT: i32 = 1024;

#[no_mangle]
pub extern "C" fn TraceExampleCustomAction(session: Session) -> CustomActionResult {
let trace_deferred_actions: Vec<&'static str> = vec!["InstallFiles"];

let database = session.database();

let custom_actions =
database.open_view("SELECT `Action`, `Type`, `Source`, `Target` FROM `CustomAction`")?;
custom_actions.execute(None)?;

let sequence_table =
database.open_view("SELECT `Action`, `Sequence` FROM `InstallExecuteSequence`")?;
sequence_table.execute(None)?;

let sequence_table_ordered = database.open_view(
"SELECT `Action`, `Sequence` FROM `InstallExecuteSequence` ORDER BY `Sequence`",
)?;
sequence_table_ordered.execute(None)?;
for action in sequence_table_ordered {
let name = action.string_data(1)?;
let sequence = action.integer_data(2).unwrap_or_default();

if trace_deferred_actions.contains(&name.as_ref()) {
let action = format!("{}Pre", name);
session.set_property(action.as_ref(), Some(action.as_ref()))?;
insert_row(&custom_actions, &sequence_table, action, sequence - 1)?;

let action = format!("{}Post", name);
session.set_property(action.as_ref(), Some(action.as_ref()))?;
insert_row(&custom_actions, &sequence_table, action, sequence + 1)?;
}
}

Succeed
}

#[no_mangle]
pub extern "C" fn TraceExampleCustomActionDeferred(session: Session) -> CustomActionResult {
let data = session.property("CustomActionData")?;
let record = Record::with_fields(Some("Running [1]"), vec![Field::StringData(data)])?;
session.message(MessageType::Info, &record);

Succeed
}

fn insert_row(
custom_actions: &View,
sequence_table: &View,
name: String,
sequence: i32,
) -> Result<()> {
const TYPE: i32 = MSIDB_CUSTOM_ACTION_TYPE_DLL
+ MSIDB_CUSTOM_ACTION_TYPE_IN_SCRIPT
+ MSIDB_CUSTOM_ACTION_TYPE_CONTINUE;

let custom_action = Record::with_fields(
None,
vec![
Field::StringData(name.clone()),
Field::IntegerData(TYPE),
Field::StringData("TraceExample".to_owned()),
Field::StringData("TraceExampleCustomActionDeferred".to_owned()),
],
)?;
custom_actions.modify(ModifyMode::InsertTemporary, &custom_action)?;

let action = Record::with_fields(
None,
vec![Field::StringData(name), Field::IntegerData(sequence)],
)?;
sequence_table.modify(ModifyMode::InsertTemporary, &action)?;
Ok(())
}
14 changes: 14 additions & 0 deletions examples/trace/trace.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
Copyright 2022 Heath Stewart.
Licensed under the MIT License. See LICENSE.txt in the project root for license information.
-->
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<Binary Id="TraceExample" SourceFile="examples\trace.dll" />
<CustomAction Id="TraceExampleCustomAction" BinaryKey="TraceExample" DllEntry="TraceExampleCustomAction" />
<InstallExecuteSequence>
<Custom Action="TraceExampleCustomAction" Sequence="1" />
</InstallExecuteSequence>
</Fragment>
</Wix>