How I implement identical feature

How I implement identical feature

Easy hack to to clone your existing feature

Instead of making a complex re-usable structure, sometimes cloning an existing feature is more wise decision. Lets see how we can we that.

Existing feature

Say, the CcrApproval feature has a file structure something like this-

/ccrApproval
    CcrApprovalContract.kt
    CcrApprovalScreen.kt
    CcrApprovalViewModel.kt

    /model
        CcrApproval.kt
        CcrApprovalResponse.kt

Now, a new feature RxApproval is to be implemented. We can do the following step to create new feature from existing one.

Steps

  1. Copy entire /ccrApproval folder and paste it (rename the package as rxApproval)

  2. Change all file names prefix from CcrApproval~ to RxApproval~

  3. Replace all occurrence of CcrApproval to RxApproval

  4. Replace all occurrence of ccrApproval to rxApproval

File name change

find . -name '*CcrApproval*.kt' -exec sh -c '
  for file do
    mv -- "$file" "${file/CcrApproval/RxApproval}"
  done
' sh {} +

Note:

  • This command will find all files (-type f) in the current directory and its subdirectories. For each of these files, it will execute the sed command to replace 'CcrApproval' with 'RxApproval' in their contents.

Content Change

CcrApproval > RxApproval

find . -type f -exec sed -i '' 's/CcrApproval/RxApproval/g' {} \;

Note:

  • the g at the end of the s/CcrApproval/RxApproval/g command tells sed to replace all occurrences of 'CcrApproval' in each file, not just the first occurrence in each line. If you only want to replace the first occurrence in each line, you can remove the g.

ccrApproval > rxApproval

find . -type f -exec sed -i '' 's/ccrApproval/rxApproval/g' {} \;

New feature

After performing all 4 steps, now we have our brand new cloned feature

/RxApprovalContract.kt
    RxApprovalScreen.kt
    RxApprovalViewModel.kt

    /model
        RxApproval.kt
        RxApprovalResponse.kt

Conclusion

Pay attention to your package declaration. Don't use IDE's feature to rename the package. It may corrupt your existing feature & references.