$30 off During Our Annual Pro Sale. View Details »

Write ruby code to change ruby code

flyerhzm
September 19, 2014

Write ruby code to change ruby code

flyerhzm

September 19, 2014
Tweet

More Decks by flyerhzm

Other Decks in Programming

Transcript

  1. Write ruby code to
    change ruby code
    Richard Huang
    Xinmin Labs

    View Slide

  2. About Me
    Founder at Xinmin Labs
    Full Stack Developer
    Open Source Enthusiast
    @flyerhzm

    View Slide

  3. Problem

    View Slide

  4. Hey, let’s use rspec new syntax for project A
    Yes, sure, I love the rspec new syntax

    View Slide

  5. Hey, let’s use rspec new syntax for project B
    Hmmm…ok

    View Slide

  6. Hey, let’s use rspec new syntax for project C
    Oh, no, I don’t want to repeat it again

    View Slide

  7. Developers love upgrading
    and refactoring code

    View Slide

  8. Developers hate changing
    code again and again

    View Slide

  9. Automation

    View Slide

  10. Metric Tools

    View Slide

  11. Metric Tools
    • cane - fails your build if code quality thresholds
    are not met
    • reek - is a tool that examines Ruby classes,
    modules and methods and reports any code
    smells it finds.
    • rails_best_practices - is a tool to check the quality
    of rails code.
    • ……

    View Slide

  12. Tell you what to do,
    but can’t do for you

    View Slide

  13. IDE

    View Slide

  14. IDE
    • can rename class / method / variable name
    • can extract methods
    • can ……

    View Slide

  15. IDE
    • can’t customize

    View Slide

  16. Based on text

    View Slide

  17. Based on text
    Replace FactoryGirl.create with create
    !
    $ sed -i '' 's/FactoryGirl.create/create/g' spec/**/*.rb

    View Slide

  18. Based on text
    Replace FactoryGirl.create with create
    !
    !

    View Slide

  19. Based on text
    Hard to be accurate
    !
    !
    !
    !

    View Slide

  20. Based on text
    Hard to convert complicated cases
    !
    !
    !
    !

    View Slide

  21. Based on AST

    View Slide

  22. What’s AST
    AST is short for Abstract Syntax Tree

    View Slide

  23. What’s AST

    View Slide

  24. Why AST
    It is much more accurate to parse ruby code.

    View Slide

  25. View Slide

  26. View Slide

  27. How to use AST
    • ripper
    • ParseTree - https://github.com/seattlerb/parsetree
    • ruby_parser - https://github.com/seattlerb/ruby_parser
    • parser - https://github.com/whitequark/parser

    View Slide

  28. How to use AST

    View Slide

  29. Example
    transpec - https://github.com/yujinakayama/
    transpec

    View Slide

  30. View Slide

  31. Based on AST
    Accurate, but hard to write and read

    View Slide

  32. Synvert
    https://github.com/xinminlabs/synvert

    View Slide

  33. Demo
    http://xinminlabs.github.io/synvert/

    View Slide

  34. View Slide

  35. Synvert
    • Add useful attributes to AST nodes
    !
    • Provide DSL to parse and rewrite code

    View Slide

  36. Attributes for AST node

    View Slide

  37. Attributes for AST node

    View Slide

  38. Attributes for AST node

    View Slide

  39. Attributes for AST node
    Check out more attributes
    http://xinminlabs.github.io/synvert/rules/

    View Slide

  40. DSL
    • within_file / within_files
    • with_node / within_node
    • if_exist_node / unless_exist_node
    • append / insert / insert_after / replace_with /
    remove
    • ……

    View Slide

  41. DSL
    Check out more DSL
    http://xinminlabs.github.io/synvert/dsl/

    View Slide

  42. View Slide

  43. Playground
    http://synvert-tools.herokuapp.com/

    View Slide

  44. Snippets
    https://github.com/xinminlabs/synvert-snippets

    View Slide

  45. Want more?
    • Fork and send pull request
    • Open issues on github
    • Hire us to refactor your project

    View Slide

  46. Q&A
    !
    Thank you

    View Slide