0

I'm following the answer given in Replace the string in file using groovy, but am getting an error. I just want to replace, for example

From
version=1.1.0

To
version=1.1.1

in some file. I do

String sv1 = '1.1.0'
String sv2 = '1.1.1'
def file = new File(`/some_path/someFile')
def fileText = file.replaceAll("version=$sv1", "version=$sv2")
file.write(fileText)

I get

Caught: groovy.lang.MissingMethodException: No signature of method: java.io.File.replaceAll() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl, org.codehaus.groovy.runtime.GStringImpl) values: [version=1.1.0, version=1.1.1]
groovy.lang.MissingMethodException: No signature of method: java.io.File.replaceAll() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl, org.codehaus.groovy.runtime.GStringImpl) values: [version=1.1.0, version=1.1.1]
    at pre-push.updatePatchVersion(pre-push:154)
    at pre-push.compareVersions(pre-push:188)
    at pre-push.run(pre-push:219)

Are the dots (.) in the string messing it up? What's the proper syntax if so? Thanks.

ldz
  • 2,217
  • 16
  • 21
Chris F
  • 14,337
  • 30
  • 94
  • 192

2 Answers2

1

You need to call replaceAll() on the file's contents, not on the File instance itself.

def fileText = file.text.replaceAll("version=$sv1", "version=$sv2")
doelleri
  • 19,232
  • 5
  • 61
  • 65
0

This will do your entire task in one line

new File(`/some_path/someFile').text=new File(`/some_path/someFile').getText().replaceAll("version=1.1.0", "version=1.1.1")
Rahul Desai
  • 189
  • 3
  • 8