Rails 5: How to add folders and files in application template script










1














I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.



In template.rb



require "fileutils"
require "shellwords"

def add_folders
mkdir views/components/buttons
mkdir csv/
end

def add_file
cd csv
touch user.csv
end

def add_readme
rm README.md
touch README.md
inject_into_file("README.md", "New readme..")
end

after_bundle do
add_folder
add_file
add_readme
end


But I don't know how to do it.










share|improve this question


























    1














    I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.



    In template.rb



    require "fileutils"
    require "shellwords"

    def add_folders
    mkdir views/components/buttons
    mkdir csv/
    end

    def add_file
    cd csv
    touch user.csv
    end

    def add_readme
    rm README.md
    touch README.md
    inject_into_file("README.md", "New readme..")
    end

    after_bundle do
    add_folder
    add_file
    add_readme
    end


    But I don't know how to do it.










    share|improve this question
























      1












      1








      1







      I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.



      In template.rb



      require "fileutils"
      require "shellwords"

      def add_folders
      mkdir views/components/buttons
      mkdir csv/
      end

      def add_file
      cd csv
      touch user.csv
      end

      def add_readme
      rm README.md
      touch README.md
      inject_into_file("README.md", "New readme..")
      end

      after_bundle do
      add_folder
      add_file
      add_readme
      end


      But I don't know how to do it.










      share|improve this question













      I would like to add folders and add files (like my own readme.md) to newly created rails apps using application templates.



      In template.rb



      require "fileutils"
      require "shellwords"

      def add_folders
      mkdir views/components/buttons
      mkdir csv/
      end

      def add_file
      cd csv
      touch user.csv
      end

      def add_readme
      rm README.md
      touch README.md
      inject_into_file("README.md", "New readme..")
      end

      after_bundle do
      add_folder
      add_file
      add_readme
      end


      But I don't know how to do it.







      ruby-on-rails-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 11:31









      Jun Dalisay

      828




      828






















          2 Answers
          2






          active

          oldest

          votes


















          0














          FileUtils covers most of what you want. mkdir_p uses the command line mkdir -p command, which makes the full path if the directories don't exist.



          IO.write (which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.



          Also, you'll want to make sure you use Rails.root.join with your file paths. It's similar to File.join, in that it helps you build a file path without doubling up your / on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join makes all of that safer.



          Here's an example of using it on a unix system:



          If Rails.root is '/some/cool/path/here', then Rails.root.join('views','components', 'buttons') would be '/some/cool/path/here/views/components/buttons'.



          require 'fileutils'
          require 'shellwords'

          def add_folders
          FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
          FileUtils.mkdir_p(Rails.root.join('csv'))
          end

          def add_file
          FileUtils.touch('Rails.root.join('csv', 'user.csv'))
          end

          def add_readme
          File.write(Rails.root.join('README.md'), 'New readme..')
          end

          after_bundle do
          add_folder
          add_file
          add_readme
          end





          share|improve this answer






















          • Thanks, I used FileUtils.mkdir_p 'csv' following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
            – Jun Dalisay
            Nov 12 at 15:56










          • @JunDalisay Be careful with that. You could end up in a bad place if you skip the Rails.root.join part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join and then use file paths that are relative to the root of your Rails app (like I showed).
            – Nate
            Nov 12 at 15:59











          • @JunDalisay I just updated my answer with some more Rails.root.join explanation.
            – Nate
            Nov 12 at 16:03


















          0














          Check whether expected directory is present or not, then create it.



          dir = "#Rails.root/public/folder_1"
          FileUtils.mkdir(dir) unless File.directory? dir


          Similarly, you can remove directory if present.



          file = "#dir/alpha.png"


          And you can further proceed with File IO in rails.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261274%2frails-5-how-to-add-folders-and-files-in-application-template-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            FileUtils covers most of what you want. mkdir_p uses the command line mkdir -p command, which makes the full path if the directories don't exist.



            IO.write (which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.



            Also, you'll want to make sure you use Rails.root.join with your file paths. It's similar to File.join, in that it helps you build a file path without doubling up your / on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join makes all of that safer.



            Here's an example of using it on a unix system:



            If Rails.root is '/some/cool/path/here', then Rails.root.join('views','components', 'buttons') would be '/some/cool/path/here/views/components/buttons'.



            require 'fileutils'
            require 'shellwords'

            def add_folders
            FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
            FileUtils.mkdir_p(Rails.root.join('csv'))
            end

            def add_file
            FileUtils.touch('Rails.root.join('csv', 'user.csv'))
            end

            def add_readme
            File.write(Rails.root.join('README.md'), 'New readme..')
            end

            after_bundle do
            add_folder
            add_file
            add_readme
            end





            share|improve this answer






















            • Thanks, I used FileUtils.mkdir_p 'csv' following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
              – Jun Dalisay
              Nov 12 at 15:56










            • @JunDalisay Be careful with that. You could end up in a bad place if you skip the Rails.root.join part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join and then use file paths that are relative to the root of your Rails app (like I showed).
              – Nate
              Nov 12 at 15:59











            • @JunDalisay I just updated my answer with some more Rails.root.join explanation.
              – Nate
              Nov 12 at 16:03















            0














            FileUtils covers most of what you want. mkdir_p uses the command line mkdir -p command, which makes the full path if the directories don't exist.



            IO.write (which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.



            Also, you'll want to make sure you use Rails.root.join with your file paths. It's similar to File.join, in that it helps you build a file path without doubling up your / on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join makes all of that safer.



            Here's an example of using it on a unix system:



            If Rails.root is '/some/cool/path/here', then Rails.root.join('views','components', 'buttons') would be '/some/cool/path/here/views/components/buttons'.



            require 'fileutils'
            require 'shellwords'

            def add_folders
            FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
            FileUtils.mkdir_p(Rails.root.join('csv'))
            end

            def add_file
            FileUtils.touch('Rails.root.join('csv', 'user.csv'))
            end

            def add_readme
            File.write(Rails.root.join('README.md'), 'New readme..')
            end

            after_bundle do
            add_folder
            add_file
            add_readme
            end





            share|improve this answer






















            • Thanks, I used FileUtils.mkdir_p 'csv' following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
              – Jun Dalisay
              Nov 12 at 15:56










            • @JunDalisay Be careful with that. You could end up in a bad place if you skip the Rails.root.join part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join and then use file paths that are relative to the root of your Rails app (like I showed).
              – Nate
              Nov 12 at 15:59











            • @JunDalisay I just updated my answer with some more Rails.root.join explanation.
              – Nate
              Nov 12 at 16:03













            0












            0








            0






            FileUtils covers most of what you want. mkdir_p uses the command line mkdir -p command, which makes the full path if the directories don't exist.



            IO.write (which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.



            Also, you'll want to make sure you use Rails.root.join with your file paths. It's similar to File.join, in that it helps you build a file path without doubling up your / on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join makes all of that safer.



            Here's an example of using it on a unix system:



            If Rails.root is '/some/cool/path/here', then Rails.root.join('views','components', 'buttons') would be '/some/cool/path/here/views/components/buttons'.



            require 'fileutils'
            require 'shellwords'

            def add_folders
            FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
            FileUtils.mkdir_p(Rails.root.join('csv'))
            end

            def add_file
            FileUtils.touch('Rails.root.join('csv', 'user.csv'))
            end

            def add_readme
            File.write(Rails.root.join('README.md'), 'New readme..')
            end

            after_bundle do
            add_folder
            add_file
            add_readme
            end





            share|improve this answer














            FileUtils covers most of what you want. mkdir_p uses the command line mkdir -p command, which makes the full path if the directories don't exist.



            IO.write (which File inherits from IO) accepts a file name, and content. No need to delete the old file and touch a new one.



            Also, you'll want to make sure you use Rails.root.join with your file paths. It's similar to File.join, in that it helps you build a file path without doubling up your / on accident, but it also returns an absolute file path on your computer. Also, it makes your code OS agnostic because while unix systems use '/' as the folder separator, Windows computers use ''. So, Rails.root.join makes all of that safer.



            Here's an example of using it on a unix system:



            If Rails.root is '/some/cool/path/here', then Rails.root.join('views','components', 'buttons') would be '/some/cool/path/here/views/components/buttons'.



            require 'fileutils'
            require 'shellwords'

            def add_folders
            FileUtils.mkdir_p(Rails.root.join('views', 'components', 'buttons'))
            FileUtils.mkdir_p(Rails.root.join('csv'))
            end

            def add_file
            FileUtils.touch('Rails.root.join('csv', 'user.csv'))
            end

            def add_readme
            File.write(Rails.root.join('README.md'), 'New readme..')
            end

            after_bundle do
            add_folder
            add_file
            add_readme
            end






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 at 16:03

























            answered Nov 12 at 13:53









            Nate

            1,118111




            1,118111











            • Thanks, I used FileUtils.mkdir_p 'csv' following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
              – Jun Dalisay
              Nov 12 at 15:56










            • @JunDalisay Be careful with that. You could end up in a bad place if you skip the Rails.root.join part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join and then use file paths that are relative to the root of your Rails app (like I showed).
              – Nate
              Nov 12 at 15:59











            • @JunDalisay I just updated my answer with some more Rails.root.join explanation.
              – Nate
              Nov 12 at 16:03
















            • Thanks, I used FileUtils.mkdir_p 'csv' following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
              – Jun Dalisay
              Nov 12 at 15:56










            • @JunDalisay Be careful with that. You could end up in a bad place if you skip the Rails.root.join part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join and then use file paths that are relative to the root of your Rails app (like I showed).
              – Nate
              Nov 12 at 15:59











            • @JunDalisay I just updated my answer with some more Rails.root.join explanation.
              – Nate
              Nov 12 at 16:03















            Thanks, I used FileUtils.mkdir_p 'csv' following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
            – Jun Dalisay
            Nov 12 at 15:56




            Thanks, I used FileUtils.mkdir_p 'csv' following this: ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html
            – Jun Dalisay
            Nov 12 at 15:56












            @JunDalisay Be careful with that. You could end up in a bad place if you skip the Rails.root.join part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join and then use file paths that are relative to the root of your Rails app (like I showed).
            – Nate
            Nov 12 at 15:59





            @JunDalisay Be careful with that. You could end up in a bad place if you skip the Rails.root.join part, because you're now assuming you know what folder you're in. Skipping that part often leads to cases of "well it works on my computer, I don't know why it isn't working on yours". It's best practice to always use Rails.root.join and then use file paths that are relative to the root of your Rails app (like I showed).
            – Nate
            Nov 12 at 15:59













            @JunDalisay I just updated my answer with some more Rails.root.join explanation.
            – Nate
            Nov 12 at 16:03




            @JunDalisay I just updated my answer with some more Rails.root.join explanation.
            – Nate
            Nov 12 at 16:03













            0














            Check whether expected directory is present or not, then create it.



            dir = "#Rails.root/public/folder_1"
            FileUtils.mkdir(dir) unless File.directory? dir


            Similarly, you can remove directory if present.



            file = "#dir/alpha.png"


            And you can further proceed with File IO in rails.






            share|improve this answer

























              0














              Check whether expected directory is present or not, then create it.



              dir = "#Rails.root/public/folder_1"
              FileUtils.mkdir(dir) unless File.directory? dir


              Similarly, you can remove directory if present.



              file = "#dir/alpha.png"


              And you can further proceed with File IO in rails.






              share|improve this answer























                0












                0








                0






                Check whether expected directory is present or not, then create it.



                dir = "#Rails.root/public/folder_1"
                FileUtils.mkdir(dir) unless File.directory? dir


                Similarly, you can remove directory if present.



                file = "#dir/alpha.png"


                And you can further proceed with File IO in rails.






                share|improve this answer












                Check whether expected directory is present or not, then create it.



                dir = "#Rails.root/public/folder_1"
                FileUtils.mkdir(dir) unless File.directory? dir


                Similarly, you can remove directory if present.



                file = "#dir/alpha.png"


                And you can further proceed with File IO in rails.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 13:20









                ray

                79412




                79412



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261274%2frails-5-how-to-add-folders-and-files-in-application-template-script%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党