phing icon indicating copy to clipboard operation
phing copied to clipboard

Cannot add line to file

Open jawira opened this issue 2 years ago • 3 comments

I'm trying to add a final new line at the end of a file. I tried to do so with AppendTask.

<append destFile="my-file.txt" text="${line.separator}"/>

The problem is this doesn't work because text is "sanitized" after:

https://github.com/phingofficial/phing/blob/0824c1412420df3dd1101effc50886718bd43d78/src/Phing/Task/System/AppendTask.php#L350-L355

I don't understand the rationale behind sanitizeText. But IMHO this method can be deleted, or maybe there's another way to simply append a new line ?

Thanks

jawira avatar Apr 21 '22 12:04 jawira

Hi @jawira did you try with fixLastLine Attribute set to true?

siad007 avatar Apr 21 '22 12:04 siad007

Hi @siad007 ! Thanks for replying :) I just tried to use fixLastLine but my build keeps failing:

BUILD FAILED
/home/jawira/PhpstormProjects/xxxxxx/build.xml:236:59 You must specify a file, use a filelist/fileset, or specify a text value.

Total time: 0.0444 seconds

I'm more curious about why sanitizeText is required? what if I just want to append white spaces?

I can make a PR if required. Since this is not very important, you can also close this issue if you want :)

jawira avatar Apr 21 '22 13:04 jawira

Hi @jawira - sry for that long delay.

Looking here a little bit closer, we have a bug here.

I'm more curious about why sanitizeText is required?

AFAIK This is needed against empty text arguments like <append destFile="my-file.txt" text=""/> (related also to xml parsing)

Things that should work are at least:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="test" default="one">
    <target name="one">
        <append destFile="my-file.txt" text="${line.separator}"/>
    </target>
    <target name="two">
        <append destFile="my-file.txt" text="
"/>
    </target>
    <target name="three">
        <append destFile="my-file.txt" text="&#10;"/>
    </target>
</project>

siad007 avatar Jun 29 '22 19:06 siad007

Actually, this is by design and matches the way the Concat task works in Ant. This is because adding a nested text element may introduce additional (ignorable) whitespace. Hence the sanitizeText call at the start of validate. This is not well documented, and maybe we need to introduce a switch that can disable that behavior.

mrook avatar Apr 05 '23 12:04 mrook

https://github.com/phingofficial/guide/commit/bad73583c55d5292cb8d926c2f2640240eede3a2

mrook avatar Apr 26 '23 13:04 mrook

@mrook thanks

jawira avatar Apr 28 '23 16:04 jawira