Giter Club home page Giter Club logo

textreplace's Introduction

TextReplace Wix Extension

TextReplace is a WiX extension that allows you to do regular expression find and replace on text files during an install I built it because I had a particular need for something similar, and I had need of a new side project to keep me amused during the long commute to work.

License

The code is licensed under the MIT License. See license.txt for details.

Building

The component was designed to be build in VS2015. It may build in earlier versions, but this hasn't been tested. It will build in VS2015 Community. Note you will require both C# and C++ libraries installed in order to build it - the extension is written in C#, but the custom action it's built on is written in C++. It just a case of loading the solution and hitting "build".

The solution will build in VS2017 Community as well, although you will need to upgrade the projects on loading for the first time.

The built extension is under the TextReplace\bin folder. You only need the TextReplace.dll.

Usage

To use text replace, you need to do two things:

  1. Add the namespace http://schemas.rottedfrog.co.uk/wix/TextReplace to your WixElement:
  2. Add TextReplace elements to the components you want to associate the change to.

The TextReplace element has 3 attributes:

Attribute Description
Id A unique name to identify the replacement
File The name of a file. This field accepts formatted values
Match An Expression to replace with
Replace A replacement expression. You can use captured numbered references $1..$9 for different captures from the match expression. $& is the whole matched expression.

Things to note about Match and replace:

The Match and Replace expressions use ECMAScript syntax for regexes and replacement strings. For more information go to https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

Note that both expressions can be read either as formatted strings or non-formatted strings. They are by default not formatted. If you add the special prefix [@@], then the expression will be read as a formatted string and you can include property values. Note that for Match, you will need to escape square brackets and braces if you make it formatted.

Example:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:tx="http://schemas.rottedfrog.co.uk/wix/TextReplace">
	<Fragment>
		<DirectoryRef Id="INSTALLFOLDER">
      <Component Id="Comp1">
        <File Id="testfile" Source="Testfile.txt" />
        <tx:TextReplace Id="text1" File="[#testfile]" Match="Hello " Replace="$&amp;$&amp;" />
      </Component>
		</DirectoryRef>
	</Fragment>
</Wix>

This will replace the string 'Hello World' in the test file with 'Hello Hello World'.

textreplace's People

Contributors

rottedfrog avatar ukamp avatar

Stargazers

perestoronin avatar  avatar Tong Zou avatar

Watchers

James Cloos avatar  avatar

textreplace's Issues

Can't run MSI using TextReplace outside of dev machine

I've built the TextReplace extension under VS 2017 on Windows 10. I included the resulting TextReplace.dll in an installer project on the same machine.

When running the installer I created on the build machine it runs fine. When I run it on a clean Windows 10 machine the install fails. The MSI log includes the following:

Action start 16:06:45: TextReplaceInitialize.
CustomAction TextReplaceInitialize returned actual error code 1157 (note this may not be 100% accurate if translation happened inside sandbox)
Error 1723. There is a problem with this Windows Installer package. A DLL required for this install to complete could not be run. Contact your support personnel or package vendor. Action TextReplaceInitialize, entry: InitTextReplace, library: C:\WINDOWS\Installer\MSI17CD.tmp 
MSI (s) (B0:48) [16:06:47:369]: Product: Test Product -- Error 1723. There is a problem with this Windows Installer package. A DLL required for this install to complete could not be run. Contact your support personnel or package vendor. Action TextReplaceInitialize, entry: InitTextReplace, library: C:\WINDOWS\Installer\MSI17CD.tmp 

Action ended 16:06:47: TextReplaceInitialize. Return value 3.
Action ended 16:06:47: INSTALL. Return value 3.

I tried installing .NET Core Runtime 2.1 on the target machine, but no change. I'm running the installer as admin and the C:\WINDOWS\Installer directory is writable.

Is there some other package that needs to be installed in order to use this extension?

Not able to build TextReplaceCA in VS 2017 Community Edition

I get the following errors while compiling msihelper.cpp & TextReplaceCA.cpp

1>c:work\repos\textreplace-master\textreplaceca\msihelper.h(310): error C2039: 'Encode': is not a member of 'MsiViewEncoder'
1>c:\work\repos\textreplace-master\textreplaceca\msihelper.h(302): note: see declaration of 'MsiViewEncoder'

  1. I have tried changing the definition from

std::wstring MsiViewEncoder::Encode(MsiView &view, Predicate filterPredicate) const
to
std::wstring Encode(MsiView &view, Predicate filterPredicate) const

but there are more issues.

  1. I have tried moving the Encode function to msihelper.cpp but I get linker errors about missing "Encode".

I haven't done C++ in more than decade so I thought I would ask for help. None of the C2039 error references from Google helped :(

======
I have copied TextReplace.dll to my project and added it as a Wix Reference. But build fails as it is looking for TextReplaceCA.dll. Based on the instructions I thought only TextReplace.dll is needed.

3>C:\Work\Repos\TextReplace-master\TextReplace.WixLib\Library.wxs(4,0): error LGHT0103: The system cannot find the file 'C:\Work\Repos\TextReplace-master\Debug\TextReplaceCA.dll'.

Property substitution not working

I'm attempting to use a property name in the Replace section, but the replacement doesn't appear to be working.

I have the following .wxs file:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:tx="http://schemas.rottedfrog.co.uk/wix/TextReplace">
	<Product Id="*" Name="setup_test_1" Language="1033" Version="1.0.0.0" Manufacturer="dbush" UpgradeCode="18373032-25e8-4990-a96a-9efce4046e4a">
		<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

		<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
		<MediaTemplate />

		<Feature Id="ProductFeature" Title="setup_test_1" Level="1">
			<ComponentGroupRef Id="ProductComponents" />
		</Feature>
		<Property Id="PROP1" Value="replacement" />
	</Product>

	<Fragment>
		<Directory Id="TARGETDIR" Name="SourceDir">
			<Directory Id="ProgramFilesFolder">
				<Directory Id="INSTALLFOLDER" Name="setup_test_1" />
			</Directory>
		</Directory>
	</Fragment>

	<Fragment>
		<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
			<Component Id="file2.txt" Guid="70FCAFDB-F265-4175-86C6-DFD6711B09A7">
				<File Id="file2.txt" Source="src/test2.txt" />
				<tx:TextReplace Id="file2.txt" File="[#file2.txt]" Match="SUBSTITUTE" Replace="[@@][PROP1]" />
			</Component>
			<Component Id="reg_key_1">
				<RegistryValue Root='HKCU' Key='SOFTWARE\setup_test_1\properties'
					Name='prop1' Value='[PROP1]'
					Type='string' />
			</Component>      
		</ComponentGroup>
	</Fragment>
</Wix>

I've got a property called PROP1 which for now has a fixed value but will eventually be updated by a UI component. To check that using the property correctly I'm referencing it when creating a registry key, and the property value "replacement" is successfully placed in the registry.

When using PROP1 in the Replace attribute for TextReplace along with the [@@] prefix to enable formatted strings, after running the MSI the file test2.txt contains the text "[PROP1]" instead of "replacement" in place of the string "SUBSTITUTE".

I tried removing the prefix which resulting in the following output from light:

C:\Users\dbush\setup_test_1\setup_test_1\Product.wxs(32) : error LGHT0204 : ICE03: Invalid format string; Table: TextReplace, Column: ReplaceExpr, Key(s): file2.txt

The MSI was still created, and this time running the MSI resulting in the string [\]PROP1[\] being inserted.

Am I doing something wrong, or is this a bug? I'm using version 3.11.2 of the WiX toolset and VS 2017.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.